Skip to content Skip to sidebar Skip to footer

How To Set Alert Text Size In Alert Dialog

The alert message by default is too big for specific devices with little screens and I want to set it to a custom dp My alert is something like this OnClickListener addNewItemListe

Solution 1:

EDIT 2:

This is the best method you can go with

AlertDialog.Builderbuilder=newBuilder(this);
builder.setMessage("Record and Images will be deleted?")
        .setTitle("MyTitle")
        .setCancelable(true)
        .setPositiveButton("Yes",
                        newDialogInterface.OnClickListener()
                        {

                            publicvoidonClick(DialogInterface dialog,
                                    int id)
                            {

                                dialog.cancel();
                                finish();
                            }
                        })
                .setNegativeButton("No",
                        newDialogInterface.OnClickListener()
                        {

                            publicvoidonClick(DialogInterface dialog,
                                    int id)
                            {
                                dialog.cancel();
                            }
                        });
        AlertDialogdialog= builder.create();
        dialog.show();
        TextViewtextView= (TextView) dialog.findViewById(android.R.id.message);
        textView.setTextSize(40);

You can use the following to get bigger text only

  alert.setMessage(Html.fromHtml("<Big>"+getString(R.string.add_message)+"</Big>"));

Note: you can use more big's for more bigger text

Solution 2:

Try this:

AlertDialogdialog=newAlertDialog.Builder(this).setMessage("Hello world").show();
TextViewtextView= (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(40);

Solution 3:

Use this code to control alertDialog

AlertDialogbuilder=newAlertDialog.Builder(getActivity())
                    .setTitle("title")
                    .setMessage("message")
                    .setPositiveButton(getResources().getString(R.string.yes), (dialogInterface, i) -> {
                        dialogInterface.dismiss();
                    })
                    .setNegativeButton(getResources().getString(R.string.cancel), (dialogInterface, i) -> dialogInterface.dismiss())


            try {

//                          /Modify Determine the font size of the Cancel button//                builder.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(26);//                builder.getButton(DialogInterface.BUTTON_NEGATIVE).setTextSize(26);// Get the mAlert objectFieldmAlert= AlertDialog.class.getDeclaredField("mAlert");
                mAlert.setAccessible(true);
                ObjectmAlertController= mAlert.get(builder);

                // Get mTitleView and set the size of the colorFieldmTitle= mAlertController.getClass().getDeclaredField("mTitleView");
                mTitle.setAccessible(true);
                TextViewmTitleView= (TextView) mTitle.get(mAlertController);
                mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen._13sdp));
                mTitleView.setTextColor(getResources().getColor(R.color.font_black));
                mTitleView.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8f, getResources().getDisplayMetrics()), 1.0f);
                mTitleView.setGravity(View.TEXT_ALIGNMENT_VIEW_START);
                mTitleView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);

                // Get mMessageView and set the size of the colorFieldmMessage= mAlertController.getClass().getDeclaredField("mMessageView");
                mMessage.setAccessible(true);
                TextViewmMessageView= (TextView) mMessage.get(mAlertController);
                mMessageView.setPadding(mMessageView.getPaddingLeft(), (int) (mMessageView.getCompoundPaddingTop() + (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 7, getResources().getDisplayMetrics()))), mMessageView.getCompoundPaddingRight(), mMessageView.getCompoundPaddingBottom());
                mMessageView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen._10sdp));
                mMessageView.setTextColor(getResources().getColor(R.color.font_black2));
                mMessageView.setLineSpacing(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8f, getResources().getDisplayMetrics()), 1.0f);
                mMessageView.setGravity(View.TEXT_ALIGNMENT_VIEW_START);
                mMessageView.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
  


            } catch (Exception e) {
                e.printStackTrace();
            }

For Proguard use this code

# for alertDialog, to customize title and message
-keep classandroidx.appcompat.app.AlertDialog{ *; }
-keep classandroidx.appcompat.app.AppCompatDialog{ *; }

Post a Comment for "How To Set Alert Text Size In Alert Dialog"