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.
Android Layout TypesTableLayout is a view that groups views into rows and columns. AbsoluteLayout enables you to specify the exact location of its children. The FrameLayout is a placeholder on screen that you can use to display a single view. ListView is a view group that displays a list of scrollable items.
The Constraint layout is the default layout used in Android.
Android Layout is used to define the user interface that holds the UI controls or widgets that will appear on the screen of an android application or activity screen. Generally, every application is a combination of View and ViewGroup.
Layout files are stored in "res-> layout" in the Android application. When we open the resource of the application we find the layout files of the Android application. We can create layouts in the XML file or in the Java file programmatically. First, we will create a new Android Studio project named "Layouts Example".
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") { ... }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With