What is the use of a ...-v21.xml?

Jdruwe picture Jdruwe · Jul 8, 2015 · Viewed 27.4k times · Source

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!

Answer

CommonsWare picture CommonsWare · Jul 8, 2015

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.