How to create an alert dialog with a multi-line header?

Is it possible to have a multi-line title in the Android alert dialog? I tried a couple of the solutions posted here, but no one worked for me. I always get a heading showing a line of 3 dots (...) for the title. Any code sample or working example regarding the same would be greatly appreciated.

+7
source share
4 answers

If you are using a warning dialog box, then the title may contain a maximum of 2 lines , otherwise you must go with a custom dialog.

+1
source

You need to use builder.setCustomTitle ():

AlertDialog.Builder builder = new AlertDialog.Builder(context); TextView textView = new TextView(context); textView.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur " + "tincidunt condimentum tristique. Vestibulum ante ante, pretium porttitor " + "iaculis vitae, congue ut sem. Curabitur ac feugiat ligula. Nulla " + "tincidunt est eu sapien iaculis rhoncus. Mauris eu risus sed justo " + "pharetra semper faucibus vel velit."); builder.setCustomTitle(textView); 

The documentation is here: AlertDialog.builder

enter image description here

+20
source

This is the way to set the title.

 AlertDialog.Builder builder = new AlertDialog.Builder(Class name.this); builder.setTitle("Welcome to App,\n There are no App.\n Add a new data."); 
+2
source

Actually, the β€œcorrect” answer is incorrect here. Turns out you can set maximum lines to more than 2 in AlertDialog. Here is an example:

 AlertDialog closePlayerDialog; ......... Builder builder = new AlertDialog.Builder(this); builder.setMessage(getString(R.string.AskToClosePlayer)) .setPositiveButton(R.string.Yes, dialogClickListener) .setNeutralButton(R.string.NoJustCloseApp, dialogClickListener) .setNegativeButton(R.string.NoContinue, dialogClickListener); closePlayerDialog = builder.create(); closePlayerDialog.setOnShowListener(new DialogInterface.OnShowListener() { public void onShow(DialogInterface dialog) { float textSize = 12.0f; Button positive = closePlayerDialog.getButton(AlertDialog.BUTTON_POSITIVE); positive.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); positive.setMaxLines(3); Button neutral = closePlayerDialog.getButton(AlertDialog.BUTTON_NEUTRAL); neutral.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); neutral.setMaxLines(3); Button negative = closePlayerDialog.getButton(AlertDialog.BUTTON_NEGATIVE); negative.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize); negative.setMaxLines(3); } }); closePlayerDialog.setCancelable(false); closePlayerDialog.show(); 

You basically edit the AlertDialog onShow components using DialogInterface.onShowListener .

0
source

All Articles