12

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.

Kenny
  • 429
  • 6
  • 22

6 Answers6

32

An extremely easy way to do this, is by setting a id and tag to the parent layout and in your onCreate(), you can findViewById() and getTag().

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_activity_view"
    android:tag="big_screen" >
        // misc views~
</RelativeLayout>

And then, in your onCreate method,

if(findViewById(R.id.my_activity_view).getTag().equals("big_screen")) {
    // do stuff
}
ssttevee
  • 423
  • 4
  • 10
  • You should be able to skip the tag test if you just give the parent layout in each of the variations a different ID. – Phil Feb 24 '19 at 22:37
22

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
}
Andrew Coonce
  • 1,557
  • 11
  • 19
3

Following code will help you. I just printed corresponding screen size or density category. U can do whatever you want!

//Determine screen size
    if ((getResources().getConfiguration().screenLayout &Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) 
    {     
        Log.d("Screen Size: ", "LARGE");

    }
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
        Log.d("Screen Size: ", "NORMAL");

    } 
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
        Log.d("Screen Size: ", "SMALL");
    }
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {     
        Log.d("Screen Size: ", "XLARGE");
    }
    else {
        Log.d("Screen Size: ","UNKNOWN_CATEGORY_SCREEN_SIZE");
    }




    //Determine density
    DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int density = metrics.densityDpi;

        if (density==DisplayMetrics.DENSITY_HIGH) {
            Log.d("Screen Density: ","HIGH");
        }
        else if (density==DisplayMetrics.DENSITY_MEDIUM) {
            Log.d("Screen Density: ","MEDIUM");
        }
        else if (density==DisplayMetrics.DENSITY_LOW) {
            Log.d("Screen Density: ","LOW");
        }
        else if (density==DisplayMetrics.DENSITY_XHIGH) {
            Log.d("Screen Density: ","XHIGH");
        }
        else if (density==DisplayMetrics.DENSITY_XXHIGH) {
            Log.d("Screen Density: ","XXHIGH");
        }
        else {
            Log.d("Screen Density: ","UNKNOWN_CATEGORY");
        }
sagarpatidar
  • 1,532
  • 1
  • 16
  • 19
2

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:

int layoutSize = getResources().getConfiguration().screenLayout;
layoutSize = layoutSize & Configuration.SCREENLAYOUT_SIZE_MASK;

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 with Configuration's pre-defined values for SCREENLAYOUT_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.

Community
  • 1
  • 1
Jon
  • 1,820
  • 2
  • 19
  • 43
2

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:

enter image description here

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 to true in landscape mode, and false 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.

Rany Albeg Wein
  • 3,304
  • 3
  • 16
  • 26
0
ViewGroup view = (ViewGroup)getWindow().getDecorView();
LinearLayout content = (LinearLayout)view.getChildAt(0);
int id = content.getId();

The id tells you which layout you're using (assuming you set the id tag in the XML).

Yusuf X
  • 14,513
  • 5
  • 35
  • 47
  • hmmm. I tried with my RelativeLayout view: `RelativeLayout content = (RelativeLayout)view.getChildAt(0);` and it causes a typecast exception: `java.lang.ClassCastException: android.widget.FrameLayout`. Any idea why? – Kenny May 21 '12 at 17:45