Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use android:Theme.Material in AppCompatActivity

First, I created two styles, one is value and the other is value-21.

In values/styles.xml is

<resources>
    <style name="AppTheme" parent="Theme.AppCompat"></style>
</resources>

In values-v21/styles.xml is

<resources>
    <style name="AppTheme" parent="android:Theme.Material"></style>
</resources>


My gradle file (app.gradle)

gradle pic1 gradle pic2


Since the ActionBarActivity is deprecated, so I use AppCompatActivity.

When I set these two style values and extend AppCompatActivity in MainActivity, I got the exception:

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

I do the two different changes and that exception never shows up again:

  • Extend "android.app.Activity" instead of v7.AppCompatActivity.
  • Change to "Theme.AppCompat" in values-v21/styles.xml.


Should I extend the "android.app.Activity" if I want to use the design theme?

Or, is there another way to use material design theme?

Thanks.

like image 820
cmingmai Avatar asked Nov 15 '25 13:11

cmingmai


1 Answers

Write the below theme in your style.xml file.

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">any_color_code</item>
    <item name="colorPrimaryDark">any_color_codek</item>
    <item name="colorAccent">any_color_code</item>
</style>

and write extends AppCompatActivity in your `MainActivity

and in the Manifest.xml file write your application tag as below:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme">
</application>
like image 188
santoshnisal007 Avatar answered Nov 17 '25 09:11

santoshnisal007



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!