How to build alert dialog with a multi-line title?
Asked Answered
N

6

10

Is it possible to have a multi-line title in an Android alert dialog? I tried a couple of solutions posted here but none worked for me. I always end up with the title showing 3 dots (...) string for title. Any sample code or working example regarding the same would be highly appreciated.

Nataline answered 2/2, 2012 at 4:8 Comment(2)
Have a look at my answer and consider marking it as correct.Micaelamicah
please consider my answer, it's annoying to have misleading "correct" answers on SO.Micaelamicah
B
-2

If you are using alert dialog then title can contain maximum 2 line, else you have to go with custom Dialog.

Blackett answered 2/2, 2012 at 4:39 Comment(3)
I tried with 3 lines title string was able to display only 2 and third string was not displayed.Nataline
This answer is incorrect and should not be the accepted answer.Mangosteen
It is possible, see my answer https://mcmap.net/q/1037293/-how-to-build-alert-dialog-with-a-multi-line-titleCellule
C
46

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);

Documentation is here: AlertDialog.builder

enter image description here

Carltoncarly answered 6/4, 2013 at 5:51 Comment(2)
This works great, only one thing, I would add some styling to the title view, for example : TextView titleView = new TextView(context);titleView.setPadding(20,10,20,10); titleView.setTypeface(null,BOLD); titleView.setTextColor(Color.BLACK);Tara
Creating title textview programmatically limits styling capabilities. It's best to create a separate xml resource file and inflate it inside setCustomTitle()Schnitzel
L
4

It seems to me that this is a cleaner solution.

Dialog theme:

<style name="CustomDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
    <item name="materialAlertDialogTitleTextStyle">@style/TitleStyle</item>
</style>

Title style:

<style name="TitleStyle" parent="@style/MaterialAlertDialog.MaterialComponents.Title.Text">
    <item name="android:maxLines">3</item>
    <item name="android:singleLine">false</item>
</style>

In code:

MaterialAlertDialogBuilder(context, R.style.CustomDialogTheme)
    .setTitle("Long title...")
    .show()

PS: You can also specify a different number, not necessarily 3. Or play around with the styles you want.

Laise answered 23/5, 2021 at 16:36 Comment(1)
Thanks! The cleanest way that works. It is also good to mention <item name ="android:ellipsize">none</item> option that manages how the ellipsis are applied to the overflowing text. The value can be one the following options: <enum name="none" value="0" /> <enum name="start" value="1" /> <enum name="middle" value="2" /> <enum name="end" value="3" /> <enum name="marquee" value="4" />Sarsen
S
1

This is the way to set 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.");
Square answered 2/2, 2012 at 4:30 Comment(1)
I think you cannot have more than 2 lines in a title bar, I tried your sample and was able to get title for 2 lines and third line was missing. What if the first string is more than 30 characters ?Nataline
C
-1

You can search the titleView by its ID within the onShowListener:

val dialog = AlertDialog.Builder(this)
    .setTitle(R.string.consent_dalog_title)
    .setMessage(getString(R.string.consent_dialog_message))
    .setCancelable(false)
    .setPositiveButton(getString(R.string.consent_dialog_permit)) { dialog, _ ->
        dialog.cancel()
    }
    .setNegativeButton(getString(R.string.constent_dialog_deny)) { dialog, _ ->
        dialog.cancel()
    }
    .create()
    dialog.setOnShowListener {
        dialog.findViewById<TextView>(R.id.alertTitle)?.let {
            it.maxLines = 5
        }
    }
    dialog.show()
Cellule answered 5/8, 2022 at 12:3 Comment(1)
This will work. Please use androidx.appcompat.R.id.alertTitle as id. Please note I have used MaterialAlertDialogBuilder. Hope it will work in other normal AlertDialog.Builder as well.Brittain
B
-2

If you are using alert dialog then title can contain maximum 2 line, else you have to go with custom Dialog.

Blackett answered 2/2, 2012 at 4:39 Comment(3)
I tried with 3 lines title string was able to display only 2 and third string was not displayed.Nataline
This answer is incorrect and should not be the accepted answer.Mangosteen
It is possible, see my answer https://mcmap.net/q/1037293/-how-to-build-alert-dialog-with-a-multi-line-titleCellule
M
-4

Actually the "correct" answer here is wrong. It 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();

Basically you edit the AlertDialog's components onShow, using DialogInterface.onShowListener.

Micaelamicah answered 29/4, 2013 at 14:25 Comment(2)
You didn't show how to change the number of lines of the title, but of the buttons. There's no dialog.getTitleBar or similar?Ramires
@LuisA.Florit Actually you're right Luis. So, in that case, you can delete the default title bar, exactly like you want, and then put your own custom textview at the top of your custom layout. The line you're looking for to delete the existing title bar is: dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);Micaelamicah

© 2022 - 2024 — McMap. All rights reserved.