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.
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.");
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With