Assume I have an activity with three different layouts in different resource folders. For example:
layout-land/my_act.xml
lay
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 either Configuration.ORIENTATION_PORTRAIT
or Configuration.ORIENTATION_LANDSCAPE
.
int size = getResources().getConfiguration().screenLayout;
- returns mask of screen size. You can test against Small, Normal, Large, xLarge sizes. For example:
if ((size & Configuration.SCREENLAYOUT_SIZE_XLARGE)==Configuration.SCREENLAYOUT_SIZE_XLARGE)
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") {
...
}