1

In an app I am developing, all I want to do is change the highlight color of an AlertDialog button. My main source of intelligence is this discussion, but I read pretty much every article about this on StackOverflow as well. The code below runs without crashing, but the highlight color of the button is still the default orange-yellow. Does anybody have an idea what is wrong?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(message).setCancelable(true)
        .setPositiveButton(getResources().getString(R.string.positive_button_title), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int whichButton) {
                // Do stuff
            }
        });
    // Incredibly bulky way of simply changing the button highlight color,
    // but it appears to be the only way we can do it without getting a NullPointerException
    AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            if (Build.VERSION.SDK_INT < 16) {
                ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundDrawable(getResources().getDrawable(R.drawable.dialog_button_drawable));
            } else {
                ((AlertDialog) dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE).setBackground(getResources().getDrawable(R.drawable.dialog_button_drawable));
            }
        }
    });
    dialog.show();

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/custom_button_background" android:state_focused="true" android:state_pressed="false" />
    <item android:drawable="@android:drawable/btn_default" android:state_focused="true" android:state_pressed="true" />
    <item android:drawable="@android:drawable/btn_default" android:state_focused="false" android:state_pressed="true" />
    <item android:drawable="@android:drawable/btn_default" />
</selector>

2 Answers2

0

Sorry if I don't get your question correctly, but since you are loading the drawable anyway, shouldn't this work for you as well?

How to change colors of a Drawable in Android?

Community
  • 1
  • 1
D4ngle
  • 21
  • 7
0

When creating the AlertDialog you can set a theme to use.

Example - Creating the Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
    builder.setTitle("AppCompatDialog");
    builder.setMessage("");
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    builder.show();

styles.xml - Custom style

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">#4CAF50</item>
</style>

In order to change the Appearance of the Title, you can do the following. First add a new style:

<style name="MyTitleTextStyle">
    <item name="android:textColor">#FFEB3B</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Title</item>
</style>

afterwards simply reference this style in your MyAlertDialogStyle:

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    ...
    <item name="android:windowTitleStyle">@style/MyTitleTextStyle</item>
</style>

This way you can define a different textColor for the message via android:textColorPrimary and a different for the title via the style.

nzala
  • 374
  • 4
  • 10