2

The recommend way to to provide layouts for different screen widths is to include each screen dependent layout in a folder using the layout-swxxdp qualifier:

res/layout-sw600dp/   # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/   # For 10” tablets (720dp wide and bigger)
res/layout-sw600dp-port/   # For 7” tablets in portrait (600dp wide or bigger)
res/layout-sw720dp-port/  # For 10” tablets in portrait (720dp wide or bigger)

Android automatically inflates the right layout according to the screen width.

That's great that Android can figure out which layout to use, but what if I have screen size dependent Java code in my activity class that I need to run? What would the implementation of the following method look like?

// Returns true if the screen width is 600 dp or more
is600dp();

I've seen methods like this

// Returns true if the screen is at least 
// approximately 480x640 dp units
private boolean isLarge(){
    return (this.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

Now what if my device has a 600dp do screen width, but the above method returns false? Android inflates the right layout, but my device specific code doesn't run.

Am I even making sense?

the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

2

Just like how you can put layouts in the res/layout-sw600dp/ folder, you can put boolean values in the res/values-sw600dp/ folder:

<resources>
  <bool name="is600dp">true</bool>
</resources>

And then reference it at runtime using

boolean is600dp = getResources().getBoolean(R.id.is600dp);
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
1

You can have one integer and have different values for that integer in the separate values folder. And get the integer value at runtime from Java code.

res/values/integers.xml

<integer name="layout_value">1</integer>

res/values-sw600dp/integers.xml

<integer name="layout_value">2</integer>

Or, get the screen width and check.

This gives you the screen width in pixels:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;

If you need only your app screen's width, in a multi-window world, you should use this:

Configuration configuration = yourActivity.getResources().getConfiguration();
int screenWidthDp = configuration.screenWidthDp; //The current width of the available screen space, in dp units, corresponding to screen width resource qualifier.
int smallestScreenWidthDp = configuration.smallestScreenWidthDp; //The smallest screen size an application will see in normal operation, corresponding to smallest screen width resource qualifier.
Bob
  • 13,447
  • 7
  • 35
  • 45
  • Fair enough, but it would be nice if there was just a constant I could access. – the_prole Aug 05 '17 at 17:45
  • I haven't seen any constants like that in SDK. Perhaps, we can write our own util methods. – Bob Aug 05 '17 at 17:46
  • I'm just afraid of rounding errors, or that not every screen is exactly that size, maybe just more or less. What if the actual or calculated screen width is like one pixel less than 600dp but Android inflates the 600dp layout? – the_prole Aug 05 '17 at 17:48
  • Great thanks, I'm assuming Android inflates the layout according to the same configuration parameter. That's really what I was looking for. – the_prole Aug 05 '17 at 17:54
  • @the_prole. I have updated my answer which has a way that does not need the screen width check. – Bob Aug 05 '17 at 17:59