Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I have Android use a qualified resource file only on older devices?

I have a separate layout file for Arabic users, that I only want to use for devices that don't support Android's native RTL mirroring (introduced in API level 17). If they are using a device with API 17 or above, I want the default XML file to be used.

How would I accomplish this? I know:

If I put my home.xml in res/layout/ then it is used as the default layout file.

If I put another home.xml in res/layout-ar/ then it will be used for arabic speakers

If I put another home.xml in res/layout-ar-v17/ then it will be used for arabic speakers with v17 or higher.*

*The problem is, I don't want to have another home.xml, I just want the system to use the default and mirror it.

like image 888
you786 Avatar asked May 08 '13 18:05

you786


People also ask

How Android finds the best matching resource?

png or background. png image is always the same, but Android selects the version of each resource that best matches the current device, by comparing the device configuration information with the qualifiers in the resource directory name.

What is purpose of resource folder in Android?

The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.

What is Alternative resources in Android?

Alternative resources are those that you've designed for use with a specific configuration. To specify that a group of resources are for a specific configuration, append an appropriate configuration qualifier to the directory name.

Where is res folder in Android Studio?

Select layouts , right-click and select New → Folder → Res Folder. This resource folder will represent a “feature category” that you want. You can easily create any type of file/folder in Android Studio.


2 Answers

You could use Layout Aliases. You would end up with two layout files, let's call them home_one.xml and home_two.xml. Then in res/values/layout.xml and the res/values-ar-v17/layout.xml you would have

<item name="home" type="layout">@layout/home_one</item>

and in res/values-ar/layout.xml you would need

<item name="home" type="layout">@layout/home_two</item>
like image 105
user1132457 Avatar answered Nov 15 '22 05:11

user1132457


I'd recommend creating two reusable layouts (as described here), one for the default layout and one for Arabic with API level 17+. Then you can define various home.xml files in appropriate configuration-specific folders so that they <include> (or <merge>) the appropriate layout. That way you only need to replication a single <include> tag instead of an entire layout.

like image 34
Ted Hopp Avatar answered Nov 15 '22 04:11

Ted Hopp