Change text color of alert dialog

gaurav tiwari picture gaurav tiwari · Jul 27, 2016 · Viewed 42.8k times · Source

I have a popup for downloading the audio instruction in my app. What I am trying to do is I want to change the default text color of "OK" to blue. I tried something but its 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 reflecting can anyone tell me what i am doing wrong?

Answer

Lino picture Lino · Jul 27, 2016

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>