Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect which layout is selected by Android in my application?

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.

like image 344
Bobs Avatar asked Jun 26 '12 10:06

Bobs


People also ask

Which types of layout is this in Android?

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.

What is the default layout in Android application *?

The Constraint layout is the default layout used in Android.

Which defines the layout of a screen in your app?

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.

Where are the layouts placed in Android?

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".


2 Answers

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") {     ... } 
like image 84
Graham Borland Avatar answered Nov 14 '22 13:11

Graham Borland


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.

like image 25
Edward Dale Avatar answered Nov 14 '22 14:11

Edward Dale