Android Programming In Kotlin: Resources
Written by Mike James   
Monday, 04 February 2019
Article Index
Android Programming In Kotlin: Resources
Conditional Resources

Conditional Resources

So far resources have just been a way to get data into your app. You might have thought that if there was another way to do the job then it might be just as good. However, Android Resources are central to customizing your app so that it works with the wide range of device types and user that an Android app has to cope with. The clever part is that you can make use of conditional resources to provide a set of resources customized for the current device at runtime. 

The idea is simple. First you provide a default resource file. This is the one that is located in the appropriate directory, /res/values say, but now you use qualifiers as part of the directory name.

For example, you can create a directory called /res/values-es which is intended to provide resource values for the app when running on a device set to a Spanish language locale. What happens is that first any values that you have defined are taken from the XML files in the values directory, these are considered the default. Next, if the app is running on a Spanish language device the XML files in values-es are processed and any resources with the same name replace the ones provided by the defaults. 

You can see that this provides a fairly easy way to make sure that your app presents a UI in the local language, but there are more qualifiers than just locale, and these allow you to customize the app for other features of the device. You can even stack up qualifiers as part of folder names to get more precise targeting. For example, the directory values-es-small would only be used if the language was Spanish and the screen was similar to a low density QVGA screen. The only thing to be careful of is that the qualifiers are used in the order in which they are listed in the documentation.

There are qualifiers for locale, screen density, size and orientation, device type, night v day, touch screen type, keyboard availability and platform version (API level). You can find them all listed in the documentation, but in most cases this is unnecessary because Android Studio helps you to create qualified resources.

If you select a resource directory, values say, and right click you will see a New,Values resource file in the context menu. If you select this option then the New Resource File dialog opens. You can use this to create a resource file – just enter its name - and you can apply any qualifiers you want to by simply selecting from the list of available quantifiers to the left:


If you select Locale then you will be allowed to enter any details of the qualifier. In the case of Locale you can select the languages and regions you want to apply from a list: 


Using the New Resource File dialog saves you a lot of time looking up qualifiers and language/region codes. It is much simpler than hand coding the directories and XML files. However, you need to be aware of how Android Studio presents qualified resources to you in the project browser. For example, if you create a new strings file in the values directory and select Locale and es for the Spanish language in Any Region then this creates a directory res/value-es and a strings.xml file within it. So now you have:




This is not what the project browser shows by default which is the Android view. It shows the new file as being in the values directory within another folder called strings.xml.

The project browser is trying to show you the resource files with all of the similar files, i.e. strings, all grouped together in a single directory, but with qualifiers displayed to the right. In the case of locale it even shows the country flag as an icon:


This is a conceptual way of organizing things and it arguably makes working with multiple resource files easier. In this case you can see that the two strings.xml files are displayed as variations on strings.xml.

You can see the actual file structure by selecting Project Files from the drop-down menu in the upper left-hand corner.


Select the Android view to return to the familiar view of your project.



Last Updated ( Monday, 04 February 2019 )