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.
If you are using alert dialog then title can contain maximum 2 line, else you have to go with custom Dialog.
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
TextView titleView = new TextView(context);titleView.setPadding(20,10,20,10); titleView.setTypeface(null,BOLD); titleView.setTextColor(Color.BLACK);
–
Tara setCustomTitle()
–
Schnitzel 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.
<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 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.");
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()
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 If you are using alert dialog then title can contain maximum 2 line, else you have to go with custom Dialog.
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
.
dialog.getTitleBar
or similar? –
Ramires © 2022 - 2024 — McMap. All rights reserved.