Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 499
John Avatar asked Jun 10 '15 06:06

John


People also ask

How do you apply styles and themes to an activity?

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.

How do I apply a theme to my Android?

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.

What is Coloronprimary in Android?

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.

What is style XML file?

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.


2 Answers

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

  • Using the Material Theme (Android docs)
like image 60
Suragch Avatar answered Oct 18 '22 05:10

Suragch


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>
like image 5
Gabriele Mariotti Avatar answered Oct 18 '22 06:10

Gabriele Mariotti