How can I detect which layout is selected by Android in my application?
Solution 1:
You can set a different android:tag
attribute on the views in each different resource file, and read the tag back at runtime with View.getTag()
.
Example:
layout-xlarge-land/my_act.xml
<View
android:id="@+id/mainview"
android:tag="xlarge-landscape"
/>
layout-xlarge/my_act.xml
<View
android:id="@+id/mainview"
android:tag="xlarge-portrait"
/>
MyActivity.java
String tag = view.getTag();
if (tag.equals("xlarge-landscape") {
...
}
Solution 2:
You could create a values-<config>
directory for each of your supported configurations. Inside of each of those directories, create a strings.xml
with a single selected_configuration
string which describes the current configuration. At runtime, fetch the string using the standard getString
method, which will do the configuration resolution for you and return the correct string for the configuration. This is untested.
Solution 3:
You can try to repeat this algorithm "How Android Finds the Best-matching Resource" - it's quite simple, especially if you have different layouts only for different screens.