Changing font size into an AlertDialog

63,562

Solution 1

You can actually get access to the message's TextView pretty easily, and then change it's size. I tested with a bigger size, but you could use whatever size you want. The text will scroll nicely as it already does. The view's id is android.R.id.message

    AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
    TextView textView = (TextView) dialog.findViewById(android.R.id.message);
    textView.setTextSize(40);

This is probably a cleaner solution, though I'm not sure if there's a risk that the TextView could be null or not.

Solution 2

I am using the following code to change the title and message text for AlertDialog…

final int alertTitle = ctx.getResources().getIdentifier("alertTitle", "id", "android");
setTitleFont((TextView) dlg.findViewById(alertTitle));
setBodyFont((TextView) dlg.findViewById(android.R.id.message));

… making sure that I check for null in my setTitleFont and setBodyFont methods.

Solution 3

For Buttons:

  final AlertDialog ad = new AlertDialog.Builder(mainScreen)
.setPositiveButton("OK", null) 
.setNegativeButton("Cancel", null).create();

ad.setOnShowListener(new DialogInterface.OnShowListener() {
                                            @Override
                                            public void onShow(DialogInterface dialog) {
                                                int textSize = (int) Helper.getDimen(mainScreen, R.dimen.textSize12);
                                                ad.getButton(Dialog.BUTTON_POSITIVE).setTextSize(textSize);
                                                ad.getButton(Dialog.BUTTON_NEGATIVE).setTextSize(textSize);
                                            }


                                      });

ad.show();

Solution 4

Here is my solution... you need to create the scroll container, then add the TextView inside the ScrollView just as you would in the XML layout.

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
String str = getString(R.string.upgrade_notes); 
final ScrollView s_view = new ScrollView(getApplicationContext());
final TextView t_view = new TextView(getApplicationContext());
t_view.setText(str);
t_view.setTextSize(14);     
s_view.addView(t_view);
alertDialog.setTitle("Upgrade notes!");
alertDialog.setView(s_view);
Share:
63,562
Waza_Be
Author by

Waza_Be

android dev

Updated on July 09, 2022

Comments

  • Waza_Be
    Waza_Be almost 2 years

    I am trying to put some loooong text into an AlertDialog. The only issue the default font size that is really too big, so I want to make it smaller.

    Here are all the workaround I tried and their issues.

    Workaround 1) Using a TextView and myView.setTextSize(12);

    final TextView myView = new TextView(getApplicationContext());
    myView.setText(myLongText);
    myView.setTextSize(12);
    final AlertDialog d = new AlertDialog.Builder(context)
        .setPositiveButton(android.R.string.ok, null)
    .setTitle(myTitle)
    .setView(myView)
    .create();
    

    Issues: Layout is not scrolling

    Workaround 2) making TextView scrollable.

    message.setMovementMethod(LinkMovementMethod.getInstance());
    

    Issues: Layout is scrolling, bute there is no "inertia" (don't know how to call that.. But I guess you understand.)

    Workaround 3) Using a Scrollview.

    That's what I am going to try, but I cannot believe there are no easier solutions...

  • gaara87
    gaara87 almost 12 years
    well, the text textView object does seem to be null
  • Some Noob Student
    Some Noob Student over 11 years
    You must call setMessage() when building the dialog, otherwise there would be no message textview in the first place.
  • Sebastian
    Sebastian about 11 years
    Actually you have to call show() too, otherwise there's no TextView. If you want to scale the buttons as well, use android.R.id.button1 etc
  • Stephen Hosking
    Stephen Hosking about 11 years
    This worked for getting the title View (thanks!), but once I got it I could only set two fonts: the standard font, and a tiny font (with 11.0 for the value).
  • jmacedo
    jmacedo almost 11 years
    This worked for me to set a font from assets folder to the title of the alert box. Good one!
  • Steven L
    Steven L almost 11 years
    Thanks a lot! Using this I was able to change the title font size as follows: int alertTitle = context.getResources().getIdentifier("alertTitle", "id", "android"); View title = dialog.findViewById(alertTitle); if (title != null && title instanceof TextView) { ((TextView) title).setTextSize(14); }
  • Lv99Zubat
    Lv99Zubat over 8 years
    what is the difference between these two methods of getting reference to the id. Why is there no android.R.id.alertTitle?
  • CoolMind
    CoolMind about 8 years
    See stackoverflow.com/a/18767270/2914140 to understand why textView can be null.
  • Anne Gunn
    Anne Gunn almost 8 years
    Not sure why this isn't getting any upvotes. It may not be an answer to the OP's direct question but as soon as you make the message bigger/smaller, you'll may well want to make the button text bigger/smaller. This seems like a nice, natural (alright, at least javascript-y) way to accomplish the task if it has to be done at runtime anyway. I combined this with the accepted good answer (stackoverflow.com/a/6563075/165164) and put those two lines inside the listener. Also, didn't bother with the getDimen call, just set a size. Don't forget to declare ad final!
  • zygimantus
    zygimantus over 7 years
    How to change that font if one has only AlertDialog.Builder object?
  • CopsOnRoad
    CopsOnRoad over 6 years
    What about the size of the Title now (android.R.id.message is for message field of the AlertDialog). What id is allocated for the Title?
  • doctorram
    doctorram over 6 years
    This solution doesn't seem to work anymore with the recent updates. findViewById() always returns null. :-(
  • Arturo Mejia
    Arturo Mejia over 5 years
    If you're using android.support.v7.app.AlertDialog, you have to use android.support.v7.appcompat.R.id.alertTitle otherwise use android.R.id.title
  • Sam Chen
    Sam Chen over 4 years
    No more working in 2020. Using custom style is the only way to workout.