I want to use the AppTheme android:Theme.Material.Light.DarkActionBar for my app. This is available from SDK 21...My minimum SDK is 16. I have the support library com.android.support:appcompat-v7:22.0.0 in my build gradle file. I want to create a second styles file under the the values folder. I want to have two styles files...one for Lollipop...greyed out version 22 next to it...and one for pre Lollipop. How to do this?
As said create a values folder for a specific Api Level
res/values-v21/styles.xml
res/values/styles.xml
In values/styles.xml create to styles:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style
<style name="AppTheme" parent="AppTheme.Base">
</style>
In values-v21/styles.xml create to styles:
<!-- Themes for Android API21 -->
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">#color/primaryColor</item>
<item name="android:colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:colorAccent">#color/accentColor</item>
</style>
Inside your AndroidManifest.xml you should use AppTheme as your global application theme
Further you should use the Toolbar from the SupportLibary instead of the default Actionbar. This is why the Base Theme sets the Actionbar to false
As suggested in the other answers you can use different styles.xml file in different folders.
I would like to add an option to other answers.
You can define a Base Style used by each versions, and customize singles attributes.
In res/values/styles.xml
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
</style>
<style name="AppTheme" parent="AppTheme.Base">
<!-- custom pre-Lollipop attributes -->
</style>
In res/values-v21/styles.xml
<style name="AppTheme" parent="AppTheme.Base">
<!-- custom Lollipop attributes -->
</style>
Finally use the AppTheme in your Activity (you have to use a ` ActionBarActivity).
You should add another folder called "values-v21" in your "res" directory. Then create a "styles.xml" within that directory for your Lollipop styles.
Create two styles.xml files in different folders, e.g:
res/values-v21/styles.xml
res/values/styles.xml
Related
Before upgrading to android 3.1.I usually just change the theme to a theme. app compact. NoTitleBar but the new version of android studio is having errors recognizing it.I solved this problem by adding base to the beginning. But I can seem to find any answer regarding a style theme that supports removing the custom bar.
In your res/values/styles.xml you have this similar block of code.
Ensure that you use Theme.AppCompat.Light.NoActionBar as parent of your theme
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Add this style in style file . where other styles are mentioned .
<!-- Base application theme. -->
<style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimaryDark</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimaryDark</item>
<item name="android:statusBarColor">#color/lightgreycolor</item>
</style>
In my case, i had to change parent theme in res/values/themes.xml
Android Studio 1.1.0. SDK 22 Installed&Compiled, SDK 17 Target (I suppose it's not relevant, because issue occurs also on preview in IDE).
I started playing with Holo theme action bar styling and ran into an issue, involving background color. This is my styles.xml:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:background">#ff0d3200</item>
<item name="android:actionBarStyle">#style/BarTheme</item>
</style>
<style name="BarTheme" parent="android:Widget.Holo.ActionBar">
<item name="android:background">#443322</item>
</style>
</resources>
And this is output. I want app title's background color to be the same as action bar's. As I've said, it's the same on the preview, so I don't think it's because my phone(SDK17) or emulator. Overflow button behaves the same way, there just isn't any in this case.
I tried setting android:titleTextStyle in BarTheme to this style:
<style name="TitleTheme" parent="android:TextAppearance.Holo">
<item name="android:background">#443322</item>
</style>
But it didn't change a thing. What do I do to overcome this?
try ,
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">value</item>
<item name="android:titleTextStyle">#style/TitleColor</item>
</style>
<style name="TitleColor" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">value</item>
</style>
Due to David's input I solved my problem myself. I had to set windowBackground, instead of background in AppTheme. I'm not sure what is the exact difference, but it works properly. Remember not to change attribute in your BarTheme, because it won't work with windowBackground. Therefore correct styles.xml file is:
<resources>
<color name="bcColor">#ff0d3200</color>
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:windowBackground">#color/bcColor</item>
<item name="android:actionBarStyle">#style/BarTheme</item>
</style>
<style name="BarTheme" parent="android:Widget.Holo.ActionBar">
<item name="android:background">#443322</item>
</style>
</resources>
Note that color has to be defined as resource in this case and cannot be hardcoded. The clean way to do this is create colors.xml in values folder and define all colors there.
Is there a performance difference in these two methods of implementing AppCompat styles? Is one method better than the other?
values/styles.xml
<style name="ActionBar.Solid.Sunshine" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/sunshine_blue_500</item>
<item name="background">#color/sunshine_blue_500</item>
</style>
---------vs--------------------
values-v14/styles.xml
<style name="ActionBar.Solid.Sunshine" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/sunshine_blue_500</item>
</style>
values/styles.xml
<style name="ActionBar.Solid.Sunshine" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/sunshine_blue_500</item>
</style>
The v-14 double folder method is better, or you'll get an error if the values/styles.xml contains a namespace that isn't accessible by the min API level
I am developing an android application that I want to support Material Design for devices running 5.0+ and devices from 4.1 to 4.4.
I am using the appcompat library to get some support for older versions.
Now I am faced with attributes that are only present in v21, like elevation.
I can create a layout-v21 folder and add my activity's layout there, again, which leads to quite a bit of duplication.
Do you do this another way?
Is there a way to use styles for this? How do I subclass a style from values to values-v21
Here is a link that I have used that gives some information about overriding styles/themes for v21 Lollipop: http://antonioleiva.com/material-design-everywhere/
Essentially you can do in values/themes.xml
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimary</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
And then in values-v21/themes.xml
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
To override AppTheme for v21 but to retain all the properties for the AppTheme.Base from pre-v21.
I am using the support v7 library to implement ActionBar in my app..I have this in my styles.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBarTheme</item>
</style>
<style name="ActionBarTheme" parent="android:Widget.ActionBar">
<item name="android:background">#FFFF0000</item>
</style>
</resources>
However, Eclipse complains in the actionBarStyle line. The error is this one:
android:actionBarStyle requires API level 11 (current min is 8)
What can I do to apply my theme to API levels 8-10?
You need to provide two API specific styles.xml. In your values/styles.xml use
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBarTheme</item>
</style>
and in your values-v14/styles.xml use
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/ActionBarTheme</item>
</style>
If you use latest v7 support library (v21 at the time of this post), there is no need to add android: prefix to any action bar attributes anymore.
In your case, adding the following to values/styles.xml will be sufficient:
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="actionBarStyle">#style/ActionBarTheme</item>
</style>
Reference: https://chris.banes.me/2014/10/17/appcompat-v21/#migration-from-previous-setup