Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 484
Manju Avatar asked Feb 02 '12 04:02

Manju


3 Answers

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.");
like image 151
Newts Avatar answered Oct 23 '22 16:10

Newts


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.

like image 29
Denys Makhov Avatar answered Oct 23 '22 17:10

Denys Makhov


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

like image 41
Yojimbo Avatar answered Oct 23 '22 16:10

Yojimbo