fireboy91
fireboy91

Reputation: 113

How to correctly use Material theme?

I'm developing an app withminSdkVersion=21. I don't wish to support older APIs below 21.

In Project structure > Dependecies, I can still see the v7 support library attached. What is the reason behind this?

What is the correct way to apply Material theme to API 21 devices because I keep on getting errors due to inflating of Coordinator layout or it tells me to use theme from Theme.AppCompat instead of android:Theme.Material.Light.DarkActionBar.

( some attributes skipped for beverity )

Manifest.xml

<application
    android:theme="@android:style/Theme.Material.Light.DarkActionBar">
    <activity
        android:name=".MainActivity"
        android:theme="@android:style/Theme.Material.Light.DarkActionBar">
    </activity>
</application>

values\styles.xml

<style name="AppTheme" parent="android:Theme.Material.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>

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

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

v21\styles.xml

<resources>
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
   <!-- item tags here -->
</style>
</resources>

build.gradle

compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "com.app.karti.themetestv3"
    minSdkVersion 21
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

Now, I'm getting 2 exceptions here : 1. Error inflating class android.support.design.widget.CoordinatorLayout 2. You need to use a Theme.AppCompat theme (or descendant) with the design library.

I'd be pleased if someone could help. Thanks.

PS : MainActivity class extends Activity and not AppCompatActivity

Upvotes: 3

Views: 2027

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

What is the reason behind this?

Because either you added it yourself or you used a template that added it. For example, the Android Studio new-project and new-activity wizard templates that ship with the IDE all use appcompat-v7.

What is the correct way to apply Material theme to API 21 devices because I keep on getting errors due to inflating of Coordinator layout or it tells me to use theme from Theme.AppCompat instead of android:Theme.Material.Light.DarkActionBar.

You cannot use CoordinatorLayout, or anything from the Android Design Support library, unless you also use appcompat-v7 and all that entails (e.g., changing your theme, using AppCompatActivity).

So, your choices are:

  1. Avoid appcompat-v7, leave your activity and stuff alone, but then don't use CoordinatorLayout, or

  2. Use appcompat-v7, or

  3. Avoid appcompat-v7, leave your activity and stuff alone, and attempt to cross-port CoordinatorLayout and anything else that you want to use from the Android Design Support library to not use appcompat-v7

I haven't tried cross-porting CoordinatorLayout. Cross-porting ActionBarDrawerToggle went smoothly. Cross-porting Snackbar looked painful, so I use an open source one that is not tied to appcompat-v7.

Upvotes: 3

Related Questions