I am developing an app for android version 4.0 and up. I just updated to appcompat 22.2.0 but I am seeing all these references to ...-v21 / v22.xml's on the web. What are they used for? Do they make a difference? Thanks in advance!
-
3Are you meaning about values-v21? values-v21 contains resources to apply on android api 21 and greater. – krystian71115 Jul 08 '15 at 21:10
-
Example: Action bar is added in api 11. To show action bar you can set another style in values-v11. – krystian71115 Jul 08 '15 at 21:13
-
For example, I had this themes.xml in the values-v21 folder when following a tutorial: http://pastebin.com/g0GrFyQT. I am not sure if I really need it or not :S – Jdruwe Jul 08 '15 at 21:15
-
If you are using Theme.AppCompat as parent of AppTheme.Base you can put it in values directory. – krystian71115 Jul 08 '15 at 21:19
-
I just deleted the -v21 version and created a 'normal' style.xml: http://pastebin.com/5P3cGFTM. Was that wrong of me to do? – Jdruwe Jul 08 '15 at 21:22
1 Answers
I had this themes.xml in the values-v21 folder
A res/values-v21/
directory contains resources that will be used when the device that is running your app is on API Level 21 or higher. If the device is running on an older version of Android, the res/values-v21/
directory will be ignored.
What are they used for? Do they make a difference?
They are used to provide different versions of resources for different versions of Android.
In the case of a themes.xml
file, an API Level 21+ device could have a theme that inherits from Theme.Material
. However, that theme does not exist on older devices. If you have a theme in res/values/
that tries to refer to Theme.Material
, your app will crash on those older devices. So, instead, you put a theme in res/values/
that will work on all devices that you are supporting (e.g., Theme.Holo
for a minSdkVersion
of 11 or higher), and override that theme in res/values-v21/
to instead use Theme.Material
.
You can see that in this sample app, where Theme.Apptheme
(my app's theme) inherits from Theme.Holo
in res/values/
and inherits from Theme.Material
in res/values-v21/
. Which version of Theme.Apptheme
is used at runtime depends on what version of Android the device has.

- 986,068
- 189
- 2,389
- 2,491
-
This explanation makes a lot of sense, thanks. In my app I reference a style that has 'Theme.AppCompat.Light' as a parent, does this mean that appcompact will take care of all this backwards compatibility so that I don't need to create an xml in a values-v21 folder? – Jdruwe Jul 08 '15 at 21:30
-
2@Jdruwe: `Theme.AppCompat` and its children (e.g., `Theme.AppCompat.Light`) work back to API Level 7, and `appcompat-v7` handles all the backwards compatibility work related to that theme. – CommonsWare Jul 08 '15 at 21:31
-
@Jdruwe No you don't need. You can create xml in values folder (not in values-v21) – krystian71115 Jul 08 '15 at 23:06
-
what if my app have styles.xml in values-v21 and also in only values folder. Which is selected for device of newer version than 21? – Shirish Herwade Jan 18 '17 at 06:20
-
3@ShirishHerwade: The name of the file does not matter. The name of the style resource matters. If you have the same style resource name defined in `values/` and `values-21/` (and nowhere else), API Level 21+ devices use the resource defined in `values-21/`, while devices running older versions of Android use the resource defined in `values/`. – CommonsWare Jan 18 '17 at 12:40
-
great answer @CommonsWare I just have two more questions: (1) Your sample app didnt had setContentView. How was it setup? (2) If we have minSDKVersion 21 do we need v21 folder? – Rushi M Thakker May 27 '17 at 06:53
-
1@RushiMThakker: The sample uses `ListActivity`. You do not need `setContentView()` with `ListActivity`, if the whole activity is a `ListView`. "If we have minSDKVersion 21 do we need v21 folder? " -- no. – CommonsWare May 27 '17 at 10:59
-
Thanks for quick reply @CommonsWare Another question: I want to set theme for my appcompatbutton I have written style with – Rushi M Thakker May 27 '17 at 11:10
-
@RushiMThakker: That has nothing to do with this question. This question is about using `Theme.Material`; you need to be using `Theme.AppCompat`. Please ask a separate Stack Overflow question, where you provide a [mcve] and a detailed explanation of what "Not working" means. – CommonsWare May 27 '17 at 11:14
-
Hey @CommonsWare I added the [question](https://stackoverflow.com/q/44216217/3484668) with all details. Please help. – Rushi M Thakker May 27 '17 at 11:20