1

I am writing an Android app where I need to put bigger text on the Buttons of Dialog windows programatically.

I saw that there are two options to set (positive, negative or neutral) buttons on an AlertDialog.Builder:

  1. setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)

  2. setPositiveButton(int textId, DialogInterface.OnClickListener listener) and

In option 1, I can only set the text and in option 2, I can use resource id of the text value. But none of the options allow me to add a styled button.

Is there some other way?

Balkrishna Rawool
  • 1,865
  • 3
  • 21
  • 35

2 Answers2

12

There is no need for you to create a custom view.

The dialog builder create method returns a AlertDialog object that gives you access to the buttons.

    AlertDialog.Builder alert = new AlertDialog.Builder(YOUR_CONTEXT);
    AlertDialog dialog = alert.create();
    // Positive
    dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextSize(THE_SIZE);
    // Negative
    dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextSize(THE_SIZE);
Carlos Paulino
  • 736
  • 6
  • 9
  • 7
    Thanks for the answer. This works great. One thing though (for aybody who is going to use this solution) that `AlertDialog.getButton()` gives null. This link [http://stackoverflow.com/questions/4604025/alertdialog-getbutton-method-gives-null-pointer-exception-android] gives more information how to avoid that. – Balkrishna Rawool Aug 10 '13 at 10:30
3

OF course, you can always go for your custom View to show to the user when the dialog is created, by using:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
builder.setView(yourCustomView);

However, take on count that creating your own custom View, means you have to inflate your own layout, create your buttons inside of it and handle the proper click actions, just like you do in a regular activity, this is the way to go if you want to modify any of the "default" views in your dialog...

Hope this helps...

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • Thanks for the answer. I actually don't need to modify the "default" look of the dialog. I just want to change the font size. So I'd prefer not to create a custom view. – Balkrishna Rawool Aug 10 '13 at 10:28