Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android material design with AppCompatActivity

To support API 19 and below I let my activities extend AppCompatActivity. I tried to set the following parent theme for v21 parent="android:Theme.Material" When I tried to ran my app it gave an exception and told me to use Activity instead of AppCompatActivity.

Does this mean I have to create new Activities which extend Activity for API 21 and above in order to get material design? Or is there a better way?

like image 369
Robin Dijkhof Avatar asked Sep 10 '15 19:09

Robin Dijkhof


2 Answers

The AppCompatActivity requires an AppCompat theme. Using a different theme, like the android:Theme.Material you will get

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

Just define a Theme in your styles.xml file:

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

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

With the AppCompat theme you can have the Material design also in device with API <21.

The android:Theme.Material can be use only with API >= 21.

like image 145
Gabriele Mariotti Avatar answered Nov 15 '22 09:11

Gabriele Mariotti


This is how I have setup my themes.xml file to support material design:

<resources>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>

</resources>

Now, in your activity, you can extend AppCompatActivity as usual and you will get the looks you want! Good luck!

like image 4
Eenvincible Avatar answered Nov 15 '22 07:11

Eenvincible