Custom theme for AlertDialog not working

11,585

Solution 1

Please check your AlertDialog import. It should be imported from v7 support lib for styles to be applied on older Android versions. I had the same problem and changing import line from

import android.app.AlertDialog

to

import android.support.v7.app.AlertDialog

helped me.

2019 Update:

Cause of google released AndroidX libraries, the new answer would be

import androidx.appcompat.app.AlertDialog;

Thx to @ChristosThemelis

Solution 2

I had the same problem and this is how I solved it:

In styles.xml, declare your Theme and set the attribute android:alertDialogTheme:

<style name="YourTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- your theme attributes here -->
    <item name="android:alertDialogTheme">@style/YourDialogTheme</item>
</style>

<style name="YourDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" >
    <!-- your dialog-theme attributes here -->
</style>

Now, if you show an AlertDialog like so...

AlertDialog.Builder builder = new AlertDialog.Builder(activity); //where activity is an Activity with the theme attribute set to android:theme="@style/YourTheme" (in AndroidManifest)
//...
builder.show();

...the dialog should have the accentColor and everything set to what you specified in @style/YourDialogTheme

Solution 3

Create a new style like this below:

<style name="AlertDialogCustom" parent="@android:style/Theme.Holo.Light.Dialog">        
  <!--buttons color-->
  <item name="colorAccent">@color/actionable_items</item>
  <!--item RadioButton or CheckBox color-->
  <item name="colorControlActivated">@color/actionable_items</item>
  <item name="colorPrimary">@color/actionable_items</item>
  <item name="colorPrimaryDark">@color/actionable_items</item>
  <item name="android:listChoiceIndicatorMultiple">@color/actionable_items</item>
  <item name="android:listChoiceIndicatorSingle">@color/actionable_items</item>
</style>

And then in your class:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom));    
AlertDialog alertDialog = dialogBuilder.create();    
alertDialog.show();
Share:
11,585
Rakesh
Author by

Rakesh

Hi, I'm Rakesh, a Staff Software Engineer with extensive experience in highly distributed &amp; scalable systems Website: http://portfolio.rakeshkumar.info

Updated on June 27, 2022

Comments

  • Rakesh
    Rakesh about 2 years

    I am trying to customize the accent color for AlertDialog buttons. But it is not taking any affect it seems it is inheriting the color from system. Here is my style/theme.

       <color name="actionable_items">#0574ac</color>  <!-- it is blue color --> 
       <style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
          <!--buttons color-->
          <item name="colorAccent">@color/actionable_items</item>
          <!--item RadioButton or CheckBox color-->
          <item name="colorControlActivated">@color/actionable_items</item>
          <item name="colorPrimary">@color/actionable_items</item>
          <item name="colorPrimaryDark">@color/actionable_items</item>
          <item name="android:listChoiceIndicatorMultiple">@color/actionable_items</item>
          <item name="android:listChoiceIndicatorSingle">@color/actionable_items</item>
       </style>
    

    Here is my code which is trying to build the Alertdialog.

    final CustomPopupBuilder removePlaceDialog =  new CustomPopupBuilder(new ContextThemeWrapper(context,
                                                                R.style.LLDialog));
                removePlaceDialog.setTitle(getString(R.string.delete_place, placeName));
                removePlaceDialog.setMessage(getString(R.string.delete_place_message));
                removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
    
                   public void onClick(DialogInterface dialog, int which) {
                      ....
                      ....
                   }
                });
                removePlaceDialog.setNegativeButton(R.string.cancel, null);
                removePlaceDialog.create().show();
    

    The final AlertDialog doesn't have buttons with the same text color. The text color is similar to green. It seems like it is inheriting the color from the system instead of the customized theme. Here is the image :enter image description here

    EDIT1:

    I tried the use the AlertDialog.Builder but it gives me the same result.

    final AlertDialog.Builder removePlaceDialog =  AlertDialog.Builder(new ContextThemeWrapper(context,
                                                                 R.style.LLDialog));
                    removePlaceDialog.setTitle(getString(R.string.delete_place, placeName));
                    removePlaceDialog.setMessage(getString(R.string.delete_place_message));
                    removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() {
    
                       public void onClick(DialogInterface dialog, int which) {
                          ....
                          ....
                       }
                    });
                    removePlaceDialog.setNegativeButton(R.string.cancel, null);
                    removePlaceDialog.create().show();
    

    Edit2:

    I also tried to change the accent color for the dialog box but I don't see that color:

    <style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
          <!--buttons color-->
          <item name="colorAccent">#990000</item>
          ...
          ...
    </style>
    

    Even this doesn't change the button text color :(.