17

I have an android app that uses some custom dialogs which are inflated from XML layouts. The contents of the dialog's view come from the XML layout, but the actual positive and negative buttons are added by calling the builder's setPositiveButton and setNegativeButton methods, so I have no control over (or at least don't know how to control) the styling of the buttons themselves.

See the onCreateDialog method below from my LoginConfirmationDialog.java file which extends DialogFragment. It basically pops a very simple dialog up that asks for confirmation of who is logging in (i.e. "Are you Joe Schmoe?", with Yes and No buttons).

The XML layout in this case has just a single TextView, and to make this easy (because the users will be construction workers with big knobby dirty fingers who need large text and large buttons), I made the font for the TextView pretty big. The two buttons though have much smaller font for their text, and since they aren't part of my layout and are added with the setPositiveButton and setNegativeButton methods, how do I control the font size?

@Override    
public Dialog onCreateDialog(Bundle savedInstanceState) {

    Bundle args = this.getArguments();

    String empName = args.getString("empName");         

    // Use the Builder class for convenient dialog construction        
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_login_confirmation, null);

    TextView message = (TextView)view.findViewById(R.id.txtLoginConfirmationMessage);
    message.setText("Are you " + empName + "?");

    builder.setView(view);      

    builder.setPositiveButton("Yes", 
            new DialogInterface.OnClickListener() {                   
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onEmpConfirmPositiveClick(LoginConfirmationDialog.this);
                }               
            });               
    builder.setNegativeButton("No", 
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mListener.onEmpConfirmNegativeClick(LoginConfirmationDialog.this);
                }
            });  

    // Create the AlertDialog object and return it        
    return builder.create();    
}
Jim
  • 6,753
  • 12
  • 44
  • 72
  • Since you are already using an xml file for the dialog why not just include the two buttons in the layout and set the `onClick` handlers in the dialog creation? – jnthnjns Apr 09 '13 at 18:51
  • I suppose that's a good point. I kind of like using the default buttons for the look and feel you get with them, but I guess I could add the buttons directly to the layout and set listeners on them. – Jim Apr 09 '13 at 18:58
  • 1
    Alternative approach is provided in this question [http://stackoverflow.com/questions/8881710/how-to-reduce-alertdialog-builder-title-font-size-and-positive-button-size][1] [1]: http://stackoverflow.com/questions/8881710/how-to-reduce-alertdialog-builder-title-font-size-and-positive-button-size – Joseph Selvaraj Apr 09 '13 at 18:59
  • @Joseph Selvaraj - thanks for the link, that also looks like a good solution. So many ways to skin a cat. – Jim Apr 10 '13 at 14:39

7 Answers7

38

Instead of returning builder.create(), try this.-

final AlertDialog alert = builder.create();
alert.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        Button btnPositive = alert.getButton(Dialog.BUTTON_POSITIVE);
        btnPositive.setTextSize(TEXT_SIZE);

        Button btnNegative = alert.getButton(Dialog.BUTTON_NEGATIVE);
        btnNegative.setTextSize(TEXT_SIZE);
    }
});

return alert;
ssantos
  • 16,001
  • 7
  • 50
  • 70
9

Took me a while, to integrate Asok's answer, since I used anonymous inner classes for buttons, so I needed to get a handle on the button references. This works. Make sure it goes after the messageDialog.show() line:

messageDialog.show();
messageDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(TypedValue.COMPLEX_UNIT_SP, 25.0f);
messageDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextSize(TypedValue.COMPLEX_UNIT_SP, 25.0f);

Note: It's recommended to use sp as a unit for text size. Unlike px, it is device density independent.

TomV
  • 1,157
  • 1
  • 13
  • 25
4

You can try this:

AlertDialog.Builder builder = new AlertDialog.Builder(CompQuestionsActivity.this);
builder.setMessage("Message");
builder.setPositiveButton("Yes", dialogClickListener);
builder.setNegativeButton("No", dialogClickListener);
AlertDialog alertDialog = builder.create();
alertDialog.show();

//For positive button:
Button button1 = alertDialog.findViewById(android.R.id.button1);
button1.setTextSize(25);
//For negative button:
Button button2 = alertDialog.findViewById(android.R.id.button2);
button2.setTextSize(25);
SO 80
  • 197
  • 2
  • 11
3

Since you are already using an xml file for the dialog why not just include the two buttons in the layout and set the onClick handlers in the dialog creation, something like this should work. I am using something similar.

Here is a quick example:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_login_confirmation, null);

TextView message = (TextView)view.findViewById(R.id.txtLoginConfirmationMessage);
message.setText("Are you " + empName + "?");

Button positiveBtn = (Button) view.findViewById(R.id.dialogButtonPositive);
    // Set size of button in relation to screen size
    positiveBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
    positiveBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onEmpConfirmPositiveClick(LoginConfirmationDialog.this);
        }
    });

Button negativeBtn = (Button) view.findViewById(R.id.dialogButtonNeg);
// Set size of button in relation to screen size
negativeBtn.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
negativeBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        mListener.onEmpConfirmNegativeClick(LoginConfirmationDialog.this);
    }
});

builder.setView(view);
return builder.create();

I am also quite fond of using the following for setting text sizes, this allows for various screen sizes to get a different size of text (You can play with the float value to suit your needs):

.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) 25);
jnthnjns
  • 8,962
  • 4
  • 42
  • 65
  • 2
    +1. This is also a great answer. I went with ssantos' solution for this one specific case I was working on, but in the future I will likely use your solution too; especially if I want the buttons to be arranged differently than the default dialog buttons. – Jim Apr 10 '13 at 14:35
2

You should check out the following answer:

In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something

You just have to change the following line of code:

<item name="android:textSize">10sp</item> to your desired size.

And don't forget to check the comments of the answer also.

Best of luck.

Community
  • 1
  • 1
Shajeel Afzal
  • 5,913
  • 6
  • 43
  • 70
2

I have tried many devices using setOnShowListener . But it did not work for all devices. In the end I come to a decision that the most easiest way is to using a Theme for your alertDialog.

Add this to the style file.

 <style name="MyAlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textSize">10sp</item>
</style>

Now Use this in your AlertDialog

  val dialog = AlertDialog.Builder(requireActivity(),R.style.MyAlertDialogTheme)

Thats all.

1

My approach is to obtain the buttons in onResume() and configure them there

public class LoginConfirmationDialog extends DialogFragment {

  @Override    
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    // your code here remains unchanged
  }

  @Override
  public void onResume() {
    super.onResume();

    Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);

    Button negativeButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_NEGATIVE);
    negativeButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
  }
}
Dino Tw
  • 3,167
  • 4
  • 34
  • 48