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
Create and apply a style To create a new style or theme, open your project's res/values/styles.xml file. For each style you want to create, follow these steps: Add a <style> element with a name that uniquely identifies the style. Add an <item> element for each style attribute you want to define.
To change default themes go to File and click on Settings. A new Settings dialog will appear, like this. Under the Appearance & Behaviour -> Appearance, you will find Theme. Choose the right theme from the drop-down and click on Apply and then Ok.
colorPrimary – The color of the app bar. colorPrimaryDark – The color of the status bar and contextual app bars; this is normally a dark version of colorPrimary. colorAccent – The color of UI controls such as check boxes, radio buttons, and edit text boxes. windowBackground – The color of the screen background.
A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the . xml extension.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With