How to inherit from a style specified from a different api version - android

This is a pretty basic question - I am supporting API versions 11+ and I want to use functionality on phones that have a newer API version. However, it would be nice if I didn't have to re-define my style. So, for example, if, in values/styles.xml I have:
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.MyApp.Input.Text">
<item name="android:layout_width">match_parent<item>
<item name="android:layout_height>wrap_content</item>
</style>
</resources>
and then in values-v14/styles.xml I have:
<resources
xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.MyApp.Input.Text">
<item name="android:textAllCaps">true</item>
</style>
</resources>
Then, on devices that have API 14+, I'll get the union of the two styles.

The question is quite old but it is possible:
The following snippet is from values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/green</item>
<item name="colorPrimaryDark">#color/dark</item>
<item name="colorAccent">#color/green</item>
<item name="checkboxStyle">#style/AppTheme.CheckBox</item>
</style>
If you want to inherit (for eg) the same style and specialize in the API >=23 you must define a values/style-23.xml as below:
<style name="AppTheme.AppTheme23" >
<item name="android:windowLightStatusBar">true</item>
</style>
Obviously in the AndroidManifest.xml the theme to specify is:
android:theme="#style/AppTheme"
That's all folks!

Related

Android styles, difference between api levels

I'm trying to learn how to Style my application using the styles.xml file and I need some clarification on a few things to understand it.
In an Item, what is the difference between setting android:actionbarstyle and just actionbarstyle ? I know that in this particular case, I have to define both, but why? And what about all other cases, for example android:colorPrimary and just colorPrimary? In that case I get an error saying that android:colorPrimary can only be used with min API level 21. So does someone have a good explanation on what the android: prefix does and how it affects my app?
Is there a reference to the different parent styles, such as parent="#style/Widget.AppCompat.Light.ActionBar and what they mean? How do I find a list of the different parent styles available for a specific item and what I can "override" in them? Right now, it's mostly guessing on my part....
Just as a reference, I'm posting my current styles.xml file.
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="MyTheme"/>
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="actionBarTheme">#style/MyTheme.ActionBarTheme</item>
<item name="android:actionBarStyle">#style/MyTheme.ActionBarStyle</item>
<item name="actionBarStyle">#style/MyTheme.ActionBarStyle</item>
<item name="colorPrimary">#color/my_green</item>
<item name="colorPrimaryDark">#color/my_forest</item>
<item name="colorAccent">#color/my_soil</item>
<item name="drawerArrowStyle">#style/MyTheme.DrawerArrowStyle</item>
<item name="android:actionOverflowButtonStyle">#style/MyTheme.OverFlow</item>
<item name="android:actionMenuTextColor">#color/white</item>
<item name="homeAsUpIndicator">#drawable/abc_ic_ab_back_mtrl_am_alpha</item>
<item name="android:homeAsUpIndicator">#drawable/abc_ic_ab_back_mtrl_am_alpha</item>
<item name="colorControlNormal">#color/my_green</item>
<item name="colorControlActivated">#color/my_forest</item>
<item name="colorControlHighlight">#color/my_deep_green</item>
</style>
<style name="MyTheme.ActionBarTheme" parent="#style/ThemeOverlay.AppCompat.ActionBar">
<!-- This sets the BACK arrow to white. Otherwise it's black. Must be placed in the theme-->
<item name="colorControlNormal">#color/white</item>
</style>
<style name="MyTheme.ActionBarStyle" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">#color/my_green</item>
<item name="background">#color/my_green</item>
<item name="android:titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="colorControlNormal">#color/white</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
<style name="MyTheme.DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#color/white</item>
</style>
<style name="MyTheme.OverFlow" parent="Widget.AppCompat.ActionButton.Overflow">
<item name="android:tint">#color/white</item>
</style>
</resources>
I will try my best to explain and will concentrate on:
<item name="colorPrimary">#color/my_green</item>
<item name="colorPrimaryDark">#color/my_forest</item>
<item name="colorAccent">#color/my_soil</item>
These attributes are regularly available with API level 21. In general you use attributes with the "android" prefix.
If you define all your styles in the styles.xml of your values folder and if you are using app compat, then you need both.
Without the prefix the attributes applies for pre L devices. i.e. App Compat. To get it work for L devices and higher you need to specifiy the attribute again with the "android" prefix.
And to get the other Android styles you can step into them, like you step into classes and the implementations. For Mac I press the command button and then click with the mouse on the specific style.

Cannot get Material Design CheckBox to use styling

I am trying to add two CheckBox views to a feedback Fragment in my current app, which is using the AppCompat library (v7:22.2.1) . The API range for this app is 16-current (22). My manifest is set up with the following theme definition:
android:theme="#style/AppTheme"
which is defined as such in my styles.xml file:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.FacebookFlatButton">
<item name="android:background">#drawable/facebook_flat_button</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#android:color/white</item>
</style>
<style name="AppTheme.TwitterFlatButton">
<item name="android:background">#drawable/twitter_flat_button</item>
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#android:color/white</item>
</style>
</resources>
The problem is my CheckBox (and I also notice my EditText) views don't default to a proper color such that they are viewable when checked. Here is a screenshot of the Preview in Android Studio and my emulator side-by-side:
No matter what combination of style items I put in this style from other SO articles such as this one, the colors don't change in the emulator or on a device, but they are updating in the Preview. I have also tried various tweaks of the parent theme to my custom theme, but nothing seems to matter.
I have tried this in a Nexus 5 (API 19) and a Nexus 6 (API 22) emulator, and it also does the same thing on my Nexus 6 device.
I'm not defining much at all custom here, but I still feel like I'm missing some small critical piece of information here. What am I missing?
OK, after much research and testing, I have a solution that works both for my CheckBox and EditText controls.
The (bad) assumption on my part was that one theme could take care of AppCompat and Material Design, which is not true. Basically, for AppCompat (< API 21), you have to define your styles with names without the android: prefix, such as this for the CheckBox and EditText:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.EditText" parent="Widget.AppCompat.EditText">
<item name="colorControlNormal">#color/almanac_red_dark</item>
<item name="colorControlActivated">#color/almanac_red_light</item>
<item name="colorControlHighlight">#color/almanac_red_light</item>
</style>
<style name="AppTheme.CheckBox">
<item name="colorAccent">#color/almanac_red_dark</item>
</style>
<style name="AppTheme.FlatButton">
<item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
<item name="android:textColor">#android:color/white</item>
</style>
<style name="AppTheme.FacebookFlatButton" parent="AppTheme.FlatButton">
<item name="android:background">#drawable/facebook_flat_button</item>
</style>
<style name="AppTheme.TwitterFlatButton" parent="AppTheme.FlatButton">
<item name="android:background">#drawable/twitter_flat_button</item>
</style>
</resources>
But you have to have another version of the style for Material Design versions (> API 21) in the values-v21 directory (with the Material Design parent theme), with the android: prefix:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="#android:style/Theme.Material.Light">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.EditText">
<item name="android:colorControlNormal">#color/almanac_red_dark</item>
<item name="android:colorControlActivated">#color/almanac_red_light</item>
<item name="android:colorControlHighlight">#color/almanac_red_light</item>
</style>
<style name="AppTheme.CheckBox">
<item name="android:colorAccent">#color/almanac_red_dark</item>
</style>
</resources>
I have tried on the Nexus 4 (API 16), Nexus 5 (API 19) and Nexus 6 (API 22) emulators and my physical Nexus 6 device and everything looks as I expect.
You need to specify the acent colors for that, put below line in your main style 'AppTheme'
<item name="colorAccent">#color/color_primary</item>
This will change your checkbox and edittext color

The different between <item name="title"> and <item name="android:title">

I make ActionBar in app.I change ActionBar style.There are some questions.
I want to change the title in the ActionBar.
So I write the follow code.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">#style/ActionBar</item>
<item name="actionBarStyle">#style/ActionBar</item>
<item name="actionOverflowButtonStyle">#style/overflowButton</item>
</style>
<!--ActionBar-->
<style name="ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/bg</item>
<item name="background">#color/bg</item>
<item name="android:title">#string/test</item>
<item name="title">#string/test</item>
</style>
<style name="overflowButton">
<item name="android:src">#mipmap/ic_add_black_48dp</item>
<item name="android:showAsAction"></item>
</style>
</resources>
when I only write <item name="android:title">#string/test</item> ,it doesn't work,but write <item name="title">#string/test</item> it work.
I have try to solve in Android Developer,but fail.Please help me.
<item name="title">#string/test</item>
this tag works on supporting library, and
<item name="android:title">#string/test</item>
this one works on your android. For more detail please have a look on here
I hope you understand, If not please let me know.
Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform's style properties (the android: properties) and once using the style properties included in the Support Library (the appcompat.R.attr properties—the context for these properties is actually your app).

AppCompat - styling using v14 folder performance

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

Applying a theme to v7 Support Action Bar

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

Categories

Resources