Search code examples
androidmaterial-designandroid-appcompatandroid-themeandroid-styles

How to use android:Theme.Material (Material theme) in styles.xml android?


In my application, I am trying to implement android:Theme.Material as a parent theme in styles values-21 folder:

 <!-- res/values-21/styles.xml -->
 <resources>
 <!-- your theme inherits from the material theme -->
 <style name="AppTheme" parent="android:Theme.Material">
    <!-- theme customizations -->
      <item name="android:colorPrimary">@color/primary</item>
    <item name="android:textColorPrimary">@color/text_primary</item>
    <!-- darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!-- theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
    <item name="android:navigationBarColor">@color/primary_dark</item>
   </style>
 </resources>

After running the app, I am getting below error

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

In values folder. I have below style

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
</style>

But, if I add the same Theme.AppCompat.Light in values-21 folder its working fine. but actionbar color is not changing.

Why can't i use the material design theme in values-21 folder? How to solve this problem?

(note: my application minsdk verison is 13 and maxsdk version is 22)

My activity extends AppCompactActivity


Solution

  • If your are using an AppCompatActivity, just use only a style in your res/values/styles.xml, and check the namespace that your are using for colorPrimary, colorPrimaryDark....

     <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
     </style>