Change title size of alertdialog in android?
15,287
Solution 1
About font size you can use this:
SpannableStringBuilder ssBuilser = new SpannableStringBuilder("Sample");
StyleSpan span = new StyleSpan(Typeface.ITALIC);
ScaleXSpan span1 = new ScaleXSpan(1);
ssBuilser.setSpan(span, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssBuilser.setSpan(span1, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(ssBuilser);
builder.show();
or
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(40);
Solution 2
Check out this link: Creating dialogs
This should help.
Or this Alert Dialog Builder
Solution 3
Here is an full example how i would do it. See the last line of code.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(cntx);
alertDialogBuilder
.setMessage(Html.fromHtml(cntx.getString(R.string.UltimateDemoText)))
.setCancelable(false)
.setPositiveButton(cntx.getString(R.string.Ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
((TextView)alertDialog.findViewById(android.R.id.title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
Solution 4
There is another way to set a new custom view to a dialog title. We can define every custom view such as TextView
and add it some custom properties and set it to the dialog title:
AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);
TextView title_of_dialog = new TextView(getApplicationContext());
title_of_dialog.setHeight(50);
title_of_dialog.setBackgroundColor(Color.RED);
title_of_dialog.setText("Custom title");
title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title_of_dialog.setTextColor(Color.WHITE);
title_of_dialog.setGravity(Gravity.CENTER);
builder.setCustomTitle(title_of_dialog);
builder.create().show();
Here, I define a dynamic TextView
and set some properties to it. Finally, I set it to dialog title using setCustomTitle()
.
Related videos on Youtube
Author by
Pol Hallen
Updated on October 24, 2022Comments
-
Pol Hallen over 1 year
about text in the message there is not problem. I can't change (personalize) color, font, style of Title of alertdialog?
What's the way?
thanks!
-
vishal almost 8 yearsHi. Getting Null Pointer Exception
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextSize(int, float)' on a null object reference
-
Primoz990 almost 8 years@vishal I have not tested this code for all api leves. If you found a solution please let me know, so i can add it to my answer.