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.
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.
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.
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.)
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.
Related
I'm trying to understand the principles of using Material Design a bit better (I'm new to this area and have been reading documentations and tutorials, but the subject is still a bit vague to me), and I'll be glad for some help. What I've tried so far is having 2 folders (values and values-21) with a styles.xml file per each of them -
values/styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>
values-v21/styles.xml:
<style name="AppTheme" parent="android:Theme.Material.NoActionBar"></style>
The problem begins where I try to use activities that inherit from AppCompatActivity (To my understanding, that's what I need to do in order to support backwards compatibility on devices before Lollipop for things such as Material Design? Am I correct? More info here would be great). If I do that, the app won't run on devices that have API 21 or above, since "AppTheme" has to inherit from AppCompat.
What I can do to solve that, is to create a base theme that inherits from AppCompat , and then make the style in both files to inherit from that base theme... But then I'm not inheriting from Material anymore.... which brings me to the question(s) -
Is Material Design just a given theme? And if so, how do I solve my problem? And what does using AppCompatActivity exactly accomplish here? Or is Material Design essentially just a set of rules and principles I'm supposed to follow? And if so, why do we need Theme.Material.* at all? I'll be glad for any extra info anyone can give me on the subject.
Thanks!
The whole point of AppCompat is that you only need one theme (you don't need a separate one in values-v21) and it will be the same experience on all devices.
Theme.AppCompat already extends android:Theme.Material on API 21+ - it is just handling all of the version checking for you.
I'm quite confused about how styling in appcompat library works.
According to here:
We now use the support implementation of Toolbar/ActionBar on all
platforms meaning that we no longer read any android: attributes
related to the action bar.
For apps which already have existing appcompat setups, this means that
you should remove your v14+ themes which re-set the same values in the
android namespace. Please note, this is ONLY applicable for
styles/widgets which affect the action bar.
For most apps, you now only need one theme declaration, in values/
So here is my question:
If I want to use material design ActionBar in API 14+, I can just use ActionBar/Toolbar provided in appcompat_v7 and style it in the common value/ folder with the "android:" namespace removed ? but why am I seeing people writing code below:
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
....
<item name="windowContentOverlay">#null</item>
<item name="android:windowContentOverlay">#null</item>
....
</style>
why is "android:" namespace there? what's the difference between the code above and using value-v21, value-v14, folders?
can someone explain or direct me to the right source?
Yes, if you use AppCompat v21+, you only need a single theme with a parent of Theme.AppCompat (or a subtheme such as Theme.AppCompat.Light) and you do not need the android: namespace attributes for action bar/window related flags, nor separate themes for v14, v20, etc. The full list of top level attributes which AppCompat provides across all API levels can be found in the AppCompat R.styleable Theme.
Much of the code on the internet (including parts of the developer.android.com site) are still written for the pre-v21 AppCompat, which did require both the android: and non-prefixed versions.
setting the sdk minimum 15, should I use Theme.Holo and not Theme.AppCompat
And instead, it is always as if I had given compatibility for Android versions 2.1 and higher.
In this guide: http://developer.android.com/training/basics/actionbar/adding-buttons.html
when I have to add the search action, the ActionBar, are forced to use xmlns: yourapp = "http://schemas.android.com/apk/res-auto"
to be able to give the right showAsAction ...
Same problem to set the Holo theme, this is not available, and returns me error (You need to use a Theme.AppCompat theme with this activity), and are forced to use AppCompat.
I then tried to create a new project with SDK minimun 16, thinking that the problem was on the 15th, and when I go to
res \ values \ styles.xml
I always find parent = "Theme.AppCompat.Light.DarkActionBar"
The only way to use Holo, is creating a project with minimum 21 SDK.
The only way to not use AppCompat is to not use AppCompat. If you extend ActionBarActivity you have to use a theme that inherits from Theme.AppCompat. If you want to use Holo themes, you have to extend Activity.
I'm generating styles using Jeff Gilfelt’s ActionBar Style Generator. Browsing the styles.xml in the different \values folders I notice that in \values-v14\styles.xml, all the item names have the android package name in them. Can someone explain why?
You don't use the android prefix for support library compatibility when using the AppCompat library, but you do if you're styling the normal ActionBar, which in this case is API level 14+.
Styling the ActionBar
I have trace Holo.Theme.Light.DarkActionBar with value-11, and see this:
<style name="Theme.Sherlock.Light.DarkActionBar" parent="Sherlock.__Theme.DarkActionBar">
...
<item name="actionBarStyle">#style/Widget.Sherlock.Light.ActionBar.Solid.Inverse</item>
...
</style>
And Theme.Sherlock.Light.DarkActionBar, is an ancestor of Holo.Theme.Light.DarkActionBar.
So, if I want to derive a new theme, why I have to cover "android:actionBarStyle" for level>=11 ?
Could anyone help me?
Thanks
ActionBarSherlock uses its own implementation to draw the ActionBar on pre level 11 devices. For this case the styles that apply to ActionBarSherlock are used. On devices with level 11 or higher, the native ActionBar is used and so only the styles for this component are used. On Android 3 or 4, the app just behaves like ActionBarSherlock would not be used in your project.