Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font size for text of dialog buttons

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();    
}
like image 323
Jim Avatar asked Apr 09 '13 18:04

Jim


People also ask

How do I change font size in dialog box?

1- Select the text or cells with text that you want to change. 2-To select all the text in a Word document, press Ctrl + e. On the Home tab, click the font size in the Font size box.

Which button increases font size?

To increase the font size, press Ctrl + ] . (Press and hold the Ctrl , then press the right bracket key.) To decrease the font size, press Ctrl + [ . (Press and hold the Ctrl , then press the left bracket key.)


2 Answers

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;
like image 169
ssantos Avatar answered Sep 28 '22 06:09

ssantos


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.

like image 34
TomV Avatar answered Sep 28 '22 07:09

TomV