Assume I have an activity with three different layouts in different resource folders. For example:
layout-land/my_act.xml
layout-xlarge/my_act.xml
layout-xlarge-land/my_act.xml
In different devices and different positions one of them is selected by Android.
How can I find out which one is selected programmatically?
Does Android have any API that returns these layouts to the program?
Edit: Graham Borland's solution has a problem in some situations that I mentioned in the comments.
I dont know the exact way to find it. But we can find it in different way.
Add one textview in all the layouts.(visibility hidden). Assign values like xlarge, land, xlarge-land accordingly.
In program, get the value from textview. Somehow we can get to know like this.
You can get info about screen orientation and size from
Resources
object. From there you can understand which layout is used.getResources().getConfiguration().orientation;
- returns eitherConfiguration.ORIENTATION_PORTRAIT
orConfiguration.ORIENTATION_LANDSCAPE
.int size = getResources().getConfiguration().screenLayout;
- returns mask of screen size. You can test against Small, Normal, Large, xLarge sizes. For example:I am assuming you are using
setContentView(int resID)
to set the content of your activities.METHOD 1 (This is my answer)
Now in all your layouts make sure that the root view always has the right tag:
example:
layout-xlarge/main.xml
:layout-small/main.xml
:Now let your activities extend this activity:
In this case, you can use the
resourceType
to know what is the resource identifier used.METHOD 2 (This was my answer but before posting I thought of the better one)
Now in all your layouts make sure that the root view always has the right tag:
example:
layout-xlarge/main.xml
:layout-small/main.xml
:Now let your activities extend this activity:
In this case all your views in your hierarchy will have the same tag.
Your question is as same as this How to get layout xml file path?
You can add a Hidden Text View with corresponding Folder names in the xml Get the String in the text view by
Make sure that all the id's of the text view are same.
Example
You can set a different
android:tag
attribute on the views in each different resource file, and read the tag back at runtime withView.getTag()
.Example:
layout-xlarge-land/my_act.xml
layout-xlarge/my_act.xml
MyActivity.java
My answer is implemented from @Graham Borland
This will work in API lavel 4 or higher.