54

I have a popup for downloading the audio instruction in my app. What I am trying to do is to change the default text color of "OK" to blue. I tried something but it's not working. Here is my code:

 private void showDownloadPgmPopup() {

    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getActivity());
    builder.setTitle("Download instructional audio?");
    builder.setMessage(ParamConstants.AUDIODOWNLOADPERMISSION);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            WwDatabaseHelper.storeSelectedWeekAndDay(getActivity(), mSelectedWeekDataModel);
            goToMoveScreen();
        }
    });
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            AndroidDownloadFileByProgressBarActivity.StartAudioAssetDownload(getActivity());

        }

    }).create();
    // change the text color of download instruction ok button


    final android.app.AlertDialog dialog = builder.show();
    dialog.setOnShowListener( new DialogInterface.OnShowListener() {
                                  @Override
                                  public void onShow(DialogInterface arg0) {
                                      dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.parseColor("#ff5722"));
                                  }
                              });
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();
}

But the change is not taking effect, can anyone tell me what I am doing wrong?

MayorJay
  • 89
  • 7
gaurav tiwari
  • 1,093
  • 1
  • 9
  • 28
  • Possible duplicate of [How to create a Custom Dialog box in android?](http://stackoverflow.com/questions/13341560/how-to-create-a-custom-dialog-box-in-android) – SaravInfern Jul 27 '16 at 09:48

10 Answers10

44

You have to provide a custom style id in the AlertDialog constructor:

AlertDialog.Builder(Context context, int themeResId)

and the style file should be something like:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:colorAccent">#0000FF</item>
</style>
Lino
  • 5,084
  • 3
  • 21
  • 39
  • Another suggestion would be theme with **`@style/MyStyle`** where `MyStyle` should be something like **``** (warning `android:alertDialogTheme` will auto-complete but does not work!) – Top-Master Oct 28 '20 at 05:43
33

Change your method to

private void showDownloadPgmPopup() {

    android.app.AlertDialog.Builder builder = new android.app
               .AlertDialog.Builder(getActivity(),R.style.AlertDialog);
...
..
.   
}

And under res/values/styles.xml add a new AlertDialog style

<style name="AlertDialog" parent="Base.Theme.AppCompat.Light.Dialog">
        <item name="android:textColor">#000000</item>
        <item name="android:textColorPrimary">#595959</item>
        <item name="android:colorAccent">#1b5e20</item>
    </style>

Below is the screen shot of these changes

See dialog box color changes

DSlomer64
  • 4,234
  • 4
  • 53
  • 88
Sushant Somani
  • 1,450
  • 3
  • 13
  • 31
15

Try calling the setTextColor after show. Refer below:

show()
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)
Spriha
  • 107
  • 6
Vahab Ghadiri
  • 2,016
  • 20
  • 26
  • 2
    Great!!! Thanks. Worked for me. As I was using material design theme none of the other answers worked. – sibin Mar 06 '19 at 16:47
7

You have two ways to do this

  1. Override default dialog.
//1. create a dialog object 'dialog'
MyCustomDialog builder = new MyCustomDialog(getActivity(), "Exit", errorMessage); 
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ...
                }

            }).create();
//2. now setup to change color of the button
dialog.setOnShowListener( new OnShowListener() {
    @Override
    public void onShow(DialogInterface arg0) {
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.parseColor("#f34235"));
    }
}

dialog.show()
  1. Create your own custom dialog
// create instance of dialog
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

// get inflater and inflate layour for dialogue 
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);

// now set layout to dialog
dialogBuilder.setView(dialogView);

// create instance like this OR directly mentioned in layout
Button button= (Button) dialogView.findViewById(R.id.label_field);
button.setText("test label");
AlertDialog alertDialog = dialogBuilder.create();

// show dialog
alertDialog.show();
Pramod Waghmare
  • 1,273
  • 13
  • 21
4

I could manage to change the text color to Red by using SpannableString in place of normal string.

Example -

var okString = new SpannableString("OK");
okString.SetSpan(new ForegroundColorSpan(Color.Red), 0, Strings.Report.Length, 0);

I then passed the variable okString to setItems().

Try passing spannableString to setPositiveButton().

2

Simply, you just change your "OK" text with this code:

Html.fromHtml("<font color='#0000FF'>OK</font>")

This code will totally change your text color depend on your added color value while #0000FF is blue.

Sarith Nob
  • 337
  • 2
  • 13
1
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.YELLOW)

The above code didn't work for me, but the below one did work:

dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.Yellow));
Spriha
  • 107
  • 6
Chris S
  • 83
  • 3
  • 12
1

With the Material Components library just define a custom style using the buttonBarPositiveButtonStyle attribute:

  <!-- Alert Dialog -->
  <style name="MaterialAlertDialog_OK_color" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <!-- Style for positive button -->
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle.textColor</item>
  </style>

  <style name="PositiveButtonStyle.textColor" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/......</item>
  </style>

Then:

 new MaterialAlertDialogBuilder(context,
        R.style.MaterialAlertDialog_OK_color)
        .setMessage("Message......")
        .setPositiveButton("ok", null)
        .setNegativeButton("Cancel", null)
        .show();

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

Here's how you can use MaterialAlertDialogBuilder to show and customise your AlertDialog.

If you want more details, should definitely check out here. https://www.journaldev.com/309/android-alert-dialog-using-kotlin

val builder = MaterialAlertDialogBuilder(requireContext())
with(builder) {
    setTitle(R.string.log_out)
    setMessage(R.string.dialog_msg_logout)
    setPositiveButton(R.string.confirm) { _, _ ->
        logoutUser()
    }
    setNegativeButton(R.string.cancel, null)
}

val dialog = builder.create()
dialog.show()

val button = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
with(button) {
    setTextColor(ContextCompat.getColor(requireContext(), R.color.colorAccent))
}

enter image description here

Morgan Koh
  • 2,297
  • 24
  • 24
0

For those looking to change the body text color, as I was, and kept finding this page, the solution is here: https://lcdsmao.medium.com/material-design-custom-alert-dialog-5a9cab3ade11

Steven L
  • 15,304
  • 1
  • 21
  • 16