Robin Dijkhof
Robin Dijkhof

Reputation: 19278

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?

Upvotes: 16

Views: 7712

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363577

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.

Upvotes: 10

Eenvincible
Eenvincible

Reputation: 5626

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!

Upvotes: 4

Related Questions