• 3

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Change text color of alert dialog

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.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            WwDatabaseHelper.storeSelectedWeekAndDay(getActivity(), mSelectedWeekDataModel);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {


    // change the text color of download instruction ok button

    final android.app.AlertDialog dialog = builder.show();
    dialog.setOnShowListener( new DialogInterface.OnShowListener() {
                                  public void onShow(DialogInterface arg0) {

But the change is not reflecting can anyone tell me what i am doing wrong?

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>
  • 30
Reply Report

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>

Below is the screen shot of these changes

See dialog box color changes

  • 17
Reply Report

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() {

                public void onClick(DialogInterface dialogInterface, int i) {

//2. now setup to change color of the button
dialog.setOnShowListener( new OnShowListener() {
    public void onShow(DialogInterface arg0) {

  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

// 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
  • 6
Reply Report

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().

  • 1
Reply Report

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

  • 0
Reply Report

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 name="PositiveButtonStyle.textColor" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@color/......</item>


 new MaterialAlertDialogBuilder(context,
        .setPositiveButton("ok", null)
        .setNegativeButton("Cancel", null)

enter image description here

  • 0
Reply Report