Styles and themes on values, values-v11 and values-v14 folders - android

I am currently working on my app to base its design on the Holo theme. Globally what I want to do is working but I am a little confused about the way that are working the folders values, values-v11 and values-v14.
So I know that:
values is targeting the API inferior to 11
values-v11 is targeting the API between 11 and 13
values-v14 is targeting the API superior to 13
At first I thought I had to specify for every folder all the styles needed for the app but then I realized a kind of inheritance system was in place.
My problem is that I am really confused and don't understand clearly how is working this inheritance between these 3 folders.
I did the following test in order to see the behavior on my phone (running on Android 4.0, so the folder values-v14 should be the one loaded):
In values I have a style to set in blue the text color:
<style name="TextMedium" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/Blue</item>
In values-v11 I have a style to set in white the text color:
<style name="TextMedium" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/White</item>
In values-v14 I have a style to set in red the text color:
<style name="TextMedium" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor">#color/Red</item>
For the first case above (every folder with a different color), the color loaded on my text is red, meaning the values-v14 folder gets the priority.
Then if I comment out the red style from the values-v14 folder, the text becomes white. Does that mean that the system will take the style in the values-v11 folder even if the device is targeting the values-v14 folder? I thought it would maybe use the values folder by default but not values-v11.
More generally, my question is, are these 3 folders working as parent and child?
Meaning that:
If the device is running on a API version > 13, the system will load values-v14 then values-v11 and finally values.
If the device is running on a API between 11 and 13, the system will load values-v11 and then values.
If the device is running on a API version < 11, the system will load only values.
If it is indeed the way it is working, does it make sense then to setup the maximum of styles in the parent folder values and add only specific ones in v11 or v14?
Sorry for the long question, I hope it is clear, this themes/styles system is only described briefly in the Android guide and it is difficult to find information on how it works...
Thanks for your help!

More generally, my question is, are these 3 folders working as parent
and child?
Those folders work with a "most specific" matching system meaning it will match the closest(lower) API level values folder:
values-v14 targets APIs >= 14(it will not be selected at all for versions below 14)
values-v11 targets APIs between(and including) 11 and 13 if values-v14 is present otherwise it will match every version starting with 11 and above((it will not be selected at all for versions below 11))
values is the default folder and it will be the last to be matched, covering other APIs levels not covered by another values-xx folder. You should always(as with all resources folders) have this folder in your app
If it is indeed the way it is working, does it make sense then to
setup the maximum of styles in the parent folder values and add only
specific ones in v11 or v14?
Yes, this is how the Android project template is built(when you use Create new project...), it actually tells you to use the values-xx folders for customization(different look, use of newer styles, attributes):
<!-- in the styles.xml from the v-14 values folder: -->
<!-- API 14 theme customizations can go here. -->

Related

Design for new API but preserve compatibility

This line style="#android:style/Widget.Holo.Light.Spinner" makes my minSdkVersion go from 1 to 11!
How can I design my views for newer versions but still display ugly views for older versions (but allowing them to get the app) ?
The spinner is not supported on old versions. You can create the specific style that uses the spinner on a folder named values-v11, and on the style default folder (only "values") design a "ugly" spinner. Note that the styles must have the same name on both folders.
Check this answer https://stackoverflow.com/a/15339215/799979
The Spinner was added in API 1. It's the style you are attempting to use that is API 11+.
To solve this, you put another styles.xml file in the values-v11 folder. Then you have the Spinner use a style from your styles.xml file. In the styles.xml file in the default values folder you have your new style inherit from the android style you want to use. In the styles.xml file in values-v11, you modify that same style to use something can be used in older APIs.

Multilanguage and multitheme at same time

I'm developing an app that has multilanguage support (using the /res/values-** way) with success. Then I want to use Holo and falling legacy devices (2.3.* for example) to use the default one (using the /res/values-v11 way).
So, I end up with a structure similar to this one (the one without language is EN, as default):
/res/values
/res/values-v11
/res/values-de
/res/values-de-v11
/res/values-es
/res/values-es-v11
... where in each one I have the following:
strings.xml
themes.xml
... where strings.xml is where the localised text are defined, and themes.xml has:
For non-v11 directories (legacy devices)
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme">
<!-- Any customizations for your app running on pre-3.0 devices here -->
</style>
</resources>
For -v11 directories (+3.0 devices)
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyTheme" parent="#android:style/Theme.Holo">
<!-- Any customizations for your app running on devices with Theme.Holo here -->
</style>
</resources>
Remembering to add this attribute into the application tag on AndroidManifest.xml
android:theme ="#style/MyTheme"
This is working in all languages and all devices, current and legacy, with correct theme picking and everything. Tested in several physical devices.
So, the question:
Don't you think that this is heavily maintainable? I mean, then we have 2 string.xml files for every language which are exactly identical, but for every new text we have to fill it twice, increasing the risk of typos. The same happens if you have analytics.xml, styles.xml, ... inside
Having the language handling so nice in Android using strings.xml, is there any other workaround to have this working multitheme and multilanguage in a nicer way?
Thank you.
I'm not sure why do you need something like
values-de-v11
I would just use something like this:
values
values-de
values-fr
values-es
values-cat
...
And put inside every strings.xml file with the translation.
On the other hand you can also add the folders:
values
values-v11
And inside you can add your themes.
The folder values should have both the strings.xml for the default language (usually english) and the fallback theme file for devices without holo.
You can check all the possibilites in the documentation:
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources

Difference between pre ICS and post ICS Holo theme related

I have a couple of app that were created with Android 1.6 and after. The problem is when I run these app on recent Android (device or simulator) like ICS, I don't have the Holo theme.
I know i can find a lot of thread on how to specify the theme but when I create a new app now, I have the new theme without any lines of code.
I don't want to have ICS theme on all android version, just ICS theme on ICS, like new holo buttons style on ICS and old grey style for older. Now I just have grey buttons for all versions.
I can create new empty projects and copy all my files into it but there must be a hidden value somewhere to chenge this.
In my manifest I have:
<uses-sdk android:minSdkVersion="3" />
<uses-sdk android:targetSdkVersion="14" />
What can be the difference between old and new created projects ?
What I'm understanding is that you want your app to switch between the legacy (classic) theme and the new Holo theme based on API version.
First, in your values (res/values) folder make a new xml. Call it styles.xml. It should contain these lines:
<resources>
<style name="AppTheme" parent="#android:style/Theme.Black"/>
</resources>
Then make a new folder in res called values-v11. In this new folder make another new xml. Also name it styles.xml This file should contain these lines:
<resources>
<style name="AppTheme" parent="#android:style/Theme.Holo"/>
</resources>
Then in your AndroidManifest.xml you Application node should contain this line:
android:theme="#style/AppTheme"
Now on devices with Honeycomb or higher you get the Holo theme and for everything else, you get the old classic theme. You can easily experiment with this to suit your needs - this is the general way to switch between themes based on API version.

Holo-themed App falls back to default Android theme

my problem is an Android app, that uses android:Theme as a default style and android:Theme.Holo.Light for OSes of version 11 and newer and android:Theme.Holo.Light.DarkActionBar for v14 and newer.
The app project was created using the ADT Eclipse-wizzard, so the AndroidManifest.xml sets android:theme="#style/AppTheme", and there are three styles.xml files, one in the values, values-v11, and values-v14 folders respectively. The default styles.xml sets
<style name="AppBaseTheme" parent="android:Theme">
the styles.xml in values-v11 sets
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
and finally styles.xml in values-v14 sets
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
This worked nicely for quite some time. I've checked the appearance for gingerbread using an emulator and the Holo-theme on a Galaxy SIII.
But now, also the Samsung phone will only use the classic android:Theme, no matter what I set in the style.xmls. I've also tried to raise the minSdkVersion in AndroidManifest.xml to 16 and to set the Holo-theme in the default styles.xml. Cleaning up and rebuilding had no effect either.
The Holo-theme is happily ignored by the app, and I don't know where else I can try looking to fix things.
Any help or pointers are very much appreciated.
Update
If I replace
android:theme="#style/AppTheme"
in AndroidManifest.xml with
android:theme="#style/AppBaseTheme"
everything works fine (AppBaseTheme is the platform-dependent parent theme for the customized platform-independent AppTheme). It somehow looks as if the Android runtime is lazy and does not care to look for the the most specific version of AppBaseTheme, but always takes the one found in values/styles.xml
Finally, after several unsuccessful cleans I took a different approach and tried to rename AppTheme to MyTheme (and updating the reference in AndroidManafest.xml accordingly). Now the app references the correct AppBaseTheme for each version of Android.

where do I place themes.xml?

In what directory do I have to place my themes.xml in order that my android recognises the correct version?
I have two versions of themes.xml. One is used by tablets. And the other one shall be used for phones with large screens. I placed the one for tablets in the folder res\values-v11\themes.xml and the other one in res\values\themes.xml
Unfortunately someting doesn't work. I definied a textColor for textViews in each of the files with different colors, so that I can recognise the file which was chosen by the OS on different phones. It worked perfectly on a xoom tablet with android 3.2. On a galaxy s2 with 2.3.5 it doesn't work.
What am I missing?
Here are the styles I use:
Tablet:
<style name='MyTheme' parent='android:Theme.Holo.Light'>
<style name='MyAutoComplete.red' parent='#style/MyAutoComplete'>
<item name='textColor'>#color/red</item>
</style>
Non-Tablet:
<style name='MyTheme' parent='android:Theme.Light.NoTitleBar'>
<style name='MyAutoComplete.blue' parent='#style/MyAutoComplete'>
<item name='textColor'>#color/blue</item>
</style>
Since your requirement is to track OS version by textView's color.
I have following suggestion. (Colors are for example only, you can change yourself)
res/values -> Yellow Color // Phone using 2.3.* or before
res/values-v11 -> Red Color // Phone using 3.0 or later
res/values-xlarge -> Green Color // Tablet using 2.3.* or before
res/values-xlarge-v11 -> Blue Color // Tablet using 3.0 or later
So a Xoom should show Blue, a S2 (2.3.*) should show Yellow, a Galaxy nexus should show Red.
You need to create a theme in styles.xml and place it in res/values folder.
Put "Tablet" in res/values-xlarge and "Non-Tablet" in res/values
You need to have two seperate xml files for the two themes to able to run the theme in the two seperate devices
Place both in resource :)
Hope it helps

Categories

Resources