Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programatically determine which XML layout my Android apps is using?

How do I programatically determine which layout (layout-normal, layout-large, etc.) my app is currently using? I see the getWindowManager().getDefaultDisplay().getMetrics(metrics) call but it seems to only deal with screen density but not necessary which layout the app is using. I need the info since I need to programatically change some of the positioning of views dynamically at runtime.

Thanks in advance.

like image 659
Kenny Avatar asked May 21 '12 16:05

Kenny


People also ask

Which is the default layout for XML in Android Studio?

The Constraint layout is the default layout used in Android.

Which directory in your application stores your XML layouts?

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the reslayout directory inside your Android project.

In which file format the layout of design is stored in Android?

xml extension, in your Android project's res/layout/ directory, so it will properly compile. More information about the syntax for a layout XML file is available in the Layout Resources document.

Which layout is 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.


1 Answers

I had the same problem. If you're using layout/layout-sw600dp/layout-sw720dp, the following ended working for me:

Configuration config = activity.getResources().getConfiguration();

if (config.smallestScreenWidthDp >= 720) {
  // sw720dp code goes here
}
else if (config.smallestScreenWidthDp >= 600) {
  // sw600dp code goes here
}
else {
  // fall-back code goes here
}
like image 77
Andrew Coonce Avatar answered Sep 27 '22 23:09

Andrew Coonce