52

I'm using ActionBarActivity from the Android 5 SDK and here is my theme.xml for v21

<style name="AppTheme_Light" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:colorPrimary">@color/abc1</item>
    <item name="android:colorPrimaryDark">@color/abc2</item>
    <item name="android:colorAccent">@color/abc3</item>
</style>

But the colors are ignored, and are replaced by a default teal color and all the dialogs appear without padding.

Problem

Also, padding is also ignored in other places like custom toast, problem only occurs in lollipop devices.

Edit:

The padding problem was due to fitsSystemWindow and I got it fixed using
this question..

But the accent color problem is still there, and it does not just affect dialogs but the whole app.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
k1slay
  • 1,068
  • 1
  • 10
  • 18

3 Answers3

128

About the accent color. You are using a AppCompat theme so you should remove Android from the namespace inside your theme.

<style name="AppTheme_Light" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/abc1</item>
    <item name="colorPrimaryDark">@color/abc2</item>
    <item name="colorAccent">@color/abc3</item>
</style>

About the dialog. AppCompat doesn't support it (as I know).
You can try to use this style in your values-v21 folder:

<style name="Theme" parent="FrameworkRoot.Theme">
    <item name="android:alertDialogTheme">@style/Theme.AlertDialog</item>
</style>

<style name="Theme.AlertDialog" parent="android:Theme.Material.Light.Dialog.Alert">
    <item name="android:colorPrimary">@color/demo_primary_color</item>
    <item name="android:colorPrimaryDark">@color/demo_colorPrimaryDark</item>
    <item name="android:colorAccent">@color/theme_accent_1</item>
</style>

UPDATE 23/04/2015: SUPPORT LIBRARY V.22.1

The new support library v22.1 works with the Dialog. You can use an android.support.v7.app.AlertDialog or the new AppCompatDialog.

For example:

import android.support.v7.app.AlertDialog

AlertDialog.Builder builder =
       new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
            builder.setTitle("Dialog");
            builder.setMessage("Lorem ipsum dolor ....");
            builder.setPositiveButton("OK", null);
            builder.setNegativeButton("Cancel", null);
            builder.show();

And use a style like this:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#FFCC00</item>
    <item name="android:textColorPrimary">#FFFFFF</item>
    <item name="android:background">#5fa3d0</item>
</style>

Otherwise you can define in your current theme:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- your style -->
    <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

and then in your code:

 import android.support.v7.app.AlertDialog

    AlertDialog.Builder builder =
           new AlertDialog.Builder(this);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 4
    Ok, first of all it's an honor to have my question answered by the creator of cards-lib. The padding problem was due to `fitsSystemWindow` and I got it fixed using [this question.](http://stackoverflow.com/questions/26599805/android-alert-dialog-not-styled-properly-on-lollipop?rq=1) Edit : Regarding the accent color it's not just limited to dialogs, but it is ignored everywhere – k1slay Oct 28 '14 at 13:05
  • 1
    Just for reference and in case anyone else runs across this. I had an issue with colorPrimary. I removed the android: and it worked fine, otherwise it was not working. The other values were working, not sure why this one had an issue. – Hatem Jaber Mar 26 '15 at 21:17
  • 2
    @GabrieleMariotti Please note that you do **not** need to pass the style to the `AlertDialog.Builder` if you define a `alertDialogTheme` attribute in your application theme. See [here](http://stackoverflow.com/a/29799142/356895). – JJD May 02 '15 at 23:20
  • 3
    in android 4.2.2 its not working dialogs and background of dialog make a look diffrent why? not lookin like lolipop dialogs – Ram Jun 17 '15 at 12:26
22

update

I have applied successfully colors for appCompat dialogs themes , maybe be helpful for someone :

values/style.xml

<style name="Theme.MyApp" parent="Theme.AppCompat.Light">

...

/* for android 4 - 4.4, we not define alert dialogs style */

</style>

values-v21/style.xml

<style name="Theme.MyApp" parent="Theme.AppCompat.Light">

...

/* define alert dialog style for android 5 */
<item name="android:alertDialogTheme">@style/Theme.AlertDialog</item>

</style>

 <style name="Theme.AlertDialog" parent="Theme.AppCompat.Light.Dialog">

    <!--app abar color in Activties Task manager -->
    <item name="colorPrimary">@color/my_color</item>

    <!--copy/paste colors -->
    <item name="colorAccent">@color/my_color</item>

    <!--status bar color -->
    <item name="colorPrimaryDark">@color/my_color</item>


</style>
Sergey Vakulenko
  • 1,655
  • 18
  • 23
  • 1
    for me this doesnt work unless I explicitly apply it! With code when initializing ProgressDialog – Pinser Jun 03 '16 at 13:21
8

Current version of AppCompat doesn't apply colorization to AlertDialogs.

Try to use https://github.com/afollestad/material-dialogs, it works great!

AlexKorovyansky
  • 4,873
  • 5
  • 37
  • 48