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

John picture John · Jun 10, 2015 · Viewed 39.8k times · Source

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

Answer

Suragch picture Suragch · Sep 6, 2016

enter image description here

Your app theme is defined in the manifest file:

<application
    android:theme="@style/AppTheme">

You will find this style defined in /res/values/styles.xml.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Using AppCompat allows your themes to be used even for devices older than Android 5.0. The main Material Design Themes are shown below.

Dark Theme

enter image description here

Light Theme

enter image description here

Light Theme with Dark Action Bar

enter image description here

Further Reading