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.
I was searching for the same thing! I found 2 similar questons/answers:
Getting ScreenLayout
Using ScreenLayout Bitmask
While all three questions are essentially the same, you really need both answers to get your result.
I used this code to get the layout size inside my Activity:
The first line will return the layout size, but it will be a number that is almost the maximum size of an
int
.The second line will AND the layout size with the given mask. The mask has a value of 15 in decimal, or F in hex.
The value returned is between 0 and 3. The values corresponding to screen sizes, as well as the value of the size mask, are defined as constants in the
android.content.res.Configuration
class: Android Configruation Documentation.All you need to do after the previous 2 lines is to have some kind of a switch statement comparing the returned
layoutSize
withConfiguration
's pre-defined values forSCREENLAYOUT_SIZE_XXX
.Hope this is not too late for you to use it. My problem was that I did not know how to ask the question in exact Android language. That is probably why the same question was, and probably should be, asked so many different ways.
That's an old question, but I would like to share my way of solving this issue.
Create a values-* variant matching each setup you wish to confirm programmatically, inside which you'll define the proper values describing the current configuration:
As you can see, the boolean value is_landscape exists both in values and values-land, therefore it is possible to safely access it via R.bool.is_landscape.
The only difference, though, is the value:
getResources().getBoolean(R.bool.is_landscape);
evaluates totrue
in landscape mode, andfalse
otherwise.Note: of course that it is possible to use
getResources().getConfiguration().orientation
in order to get the current device orientation, though I chose to use this simple familiar case to make my example clear and straight forward.It is important to understand that this technique is versatile, using which one can define a more complex values resource directory, ( e.g. values-ldrtl-sw600dp-v23 ) to support a more specific configuration.
I had the same problem. If you're using
layout
/layout-sw600dp
/layout-sw720dp
, the following ended working for me:The id tells you which layout you're using (assuming you set the id tag in the XML).
An extremely easy way to do this, is by setting a id and tag to the parent layout and in your
onCreate()
, you canfindViewById()
andgetTag()
.And then, in your
onCreate
method,Following code will help you. I just printed corresponding screen size or density category. U can do whatever you want!