Changing font size into an AlertDialog

后端 未结 7 2205
广开言路
广开言路 2020-12-04 22:10

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

相关标签:
7条回答
  • 2020-12-04 22:21

    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.

    0 讨论(0)
  • 2020-12-04 22:21

    You can solve this issue by using SpannableStringBuilder instead of a string.

    http://www.android--tutorials.com/2016/10/android-change-alertdialog-title-text_21.html

    0 讨论(0)
  • 2020-12-04 22:26

    This works in 2020. I have tested this in Android 10

    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.YourStyle) //Style is only needed if you want to customize look&feel
                .setTitle(R.string.title)
                .setMessage(R.string.message)
                .setPositiveButton(R.string.accept, null);
        AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(dialog -> {
            //The following is to style dialog message
            TextView message = alertDialog.findViewById(android.R.id.message);
            if (message != null) {
                message.setTextSize(12);
                message.setTextColor(getColor(R.color.yellow));
            }
            //The following is to style dialog title. Note that id is alertTitle
            TextView title = alertDialog.findViewById(R.id.alertTitle);
            if (title != null) {
                title.setTextColor(getColor(R.color.black));
            }
        });
        alertDialog.show();
    
    0 讨论(0)
  • 2020-12-04 22:31

    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.

    0 讨论(0)
  • 2020-12-04 22:31
                   AlertDialog.Builder builder = new 
                    AlertDialog.Builder(YourActivity.this);
                    builder.setTitle("Your Title");
                    builder.setMessage("Your Message");
                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            // Your Positive Function
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            // Your Negative Function
                        }
                    });
                    builder.show();
    
    0 讨论(0)
  • 2020-12-04 22:32

    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();
    
    0 讨论(0)
提交回复
热议问题