How to set font size for text of dialog buttons

拈花ヽ惹草 提交于 2019-11-30 18:45:54

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;

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.

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);
Shajeel Afzal

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.

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);

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);
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!