SoFunction
Updated on 2025-03-11

How to change the title and button color of Dialog

Preface

This article mainly introduces to you how to change the title and button color of Dialog. It is shared for your reference and learning. I won’t say much below, let’s take a look at the detailed introduction together.

.

The first line in this class defines the following variables:

final AlertController mAlert;

The specific implementation of AlertDialog's functions are encapsulated internally in this AlertController.

Modify button color

1.

public Button getButton(int whichButton) {
 return (whichButton);
 }

There are three types of parameters whichButton:

  • DialogInterface.BUTTON_POSITIVE
  • DialogInterface.BUTTON_NEGATIVE
  • DialogInterface.BUTTON_NEUTRAL

Pass the corresponding parameters to get the corresponding Button

Button btnPositive = (Button)(DialogInterface.BUTTON_POSITIVE);
(color);

This method can only set the color of the button, but cannot set the title color

2

The constructor of AlertDialog is as follows:

protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
 super(context, resolveDialogTheme(context, themeResId));
 mAlert = new AlertController(getContext(), this, getWindow());
 }

Here, the AlertController is initialized and passed ingetWindow(),thisgetWindow()It is the method inherited from Dialog. The method is as follows:

#()
 public @Nullable Window getWindow() {
 return mWindow;
 }

After passing this window object into the AlertController, you can see the dialog title and button id in the AlertController source code, and pass(id)Get the corresponding View.

So here you can get the title and button of the dialog box like this:

//titleTextView tvTitle = (TextView)().findViewById();
//ButtonButton btnPositive = (Button)().findViewById(.button1);

Then set the required color. This method can modify the font color of all controls with id set in Dialog.

3 Reflections

3.1 First get the AlertController object

 Field mAlert = ("mAlert");
 (true);
 Object controller = (dialog);

Find titles and buttons that need to change the font color inside AlertController

Button mButtonPositive;
Button mButtonNegative;
Button mButtonNeutral;
private TextView mTitleView;
private TextView mMessageView;

Then obtain the corresponding control through reflection and modify the control color

 Field mTitleView = ().getDeclaredField("mTitleView");
 (true);
 TextView tvTitle = (TextView) (controller);
 ();//Change the color of the title

Compared with the three methods, the second one is the simplest and the most efficient

Change the location where Dialog is displayed

Window window = ();
  lp = ();
 = ;
 = 100;
 = 100;
(lp);

What to note here is thatThe x and y coordinates are as follows:

 /**
  * X position for this window. With the default gravity it is ignored.
  * When using {@link Gravity#LEFT} or {@link Gravity#START} or {@link Gravity#RIGHT} or
  * {@link Gravity#END} it provides an offset from the given edge.
  */
 @
 public int x;

 /**
  * Y position for this window. With the default gravity it is ignored.
  * When using {@link Gravity#TOP} or {@link Gravity#BOTTOM} it provides
  * an offset from the given edge.
  */
 @
 public int y;

If it is the default, then x and y are invalid even if set. Therefore, x and y need toOnly when used in combination will it work. Of courseCan also be used alone.

Summarize

The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.