android:Theme.Material.Light requires API level 21 (current min is 8)

43,446

Solution 1

For this you need to have 2 values folders.

One that exists by default, and another, you have to create in your res folder and name it values-v21.

In the default values folder, in styles.xml, use a theme other than Material theme. And in the styles.xml of values-v21 folder that you created, use Material theme.

Android phone will automatically pickup the styles.xml which it supports. If the phone supports Material Design (Lollipop devices), your app will use Material theme (values-21 folder).

If it doesn't (in phones running older Android versions), the default values folder will be used.

Solution 2

You need to use android:theme="@style/Theme.AppCompat.Light" theme to get a material design.

Make sure your min is 8 and your target is 21. And you're using build tools/sdk 21.

Solution 3

Pedro Oliveira is right with regards to Theme.AppCompat, but some essential information is missing in that answer.

A blog post titled appcompat v21: material design for pre-Lollipop devices! by Chris Banes from the Android team probably best answers the question of how to get Material Theme for API levels prior to 21.

To summarise, you need appcompat-v7 dependency:

dependencies {
    ...
    compile "com.android.support:appcompat-v7:21.0.3"
}

After that, for dark version as your base theme, use:

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

And for light version:

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

And if you're new to AppCompat, there are things you need to know, such as:

All of your Activities must extend from ActionBarActivity*. It extends from FragmentActivity from the v4 support library, so you can continue to use fragments.

*NB: more recently, ActionBarActivity has been deprecated in favour of AppCompatActivity.

But you really should read the whole Setup section of that blog post! (The information on Toolbar vs Action Bar, and some of the comments are also something you probably shouldn't miss.)

Solution 4

In your NameActivity.java file import the following:

import android.support.v7.widget.Toolbar;

Comment the previous one:

//import android.widget.Toolbar;

With this the problem is solved.

Share:
43,446
PPD
Author by

PPD

Updated on July 09, 2022

Comments

  • PPD
    PPD almost 2 years

    I want to use Material Theme in my application which has minimum sdk version of 8. As per docs - "The material theme is only available in Android 5.0 (API level 21) and above. The v7 Support Libraries provide themes with material design styles for some widgets and support for customizing the color palette." Does it mean I can use it if I add v7 Support Libarary in my project? Because after adding this library I got the following error:

    android:Theme.Material.Light requires API level 21 (current min is 8).

    Or maybe I understood something wrong? Any suggestion will be appreciated. Thanks in advance.

  • Rishabh
    Rishabh over 9 years
    I think I prefer this answer over the accepted one.
  • Pedro Oliveira
    Pedro Oliveira about 9 years
    Thanks for adding that up :)
  • Evan Leis
    Evan Leis about 9 years
    Using Theme.AppCompat.Light.NoActionBar and extending from android.support.v4.app.FragmentActivity, it works fine. I think once you add the ActionBar, the requirement to extend from ActionBarActivity comes into play.
  • Jonik
    Jonik about 9 years
    Yeah, that wording I quoted is not the clearest. If you don't use Action Bar (or Toolbar as Action Bar?), ActionBarActivity is not needed.
  • Koen Demonie
    Koen Demonie about 9 years
    would also have to make an identical Strings.xml ? seems abit redundant ?
  • Someone Somewhere
    Someone Somewhere over 8 years
    nice summary. Thanks !
  • laalto
    laalto almost 6 years
    I don't think this is an answer to the question here.