0

I'm aware that I can set different layouts for different screen sizes, but I would like to pull out the information of which one it has chosen. For example in the activity layout file for large tablets, I would like to add a view that scrolls through different statistical stuff, but on smaller phones this won't look good, and I'd rather have nothing there, as this will be a menu (i.e. fragments are overkill and will distract from the actual purpose)

So I would like to write some code which says something like:

if(xlarge) { (manipulate stuff) }

Is it possible to pull out the right constants from somewhere?

Imran
  • 413
  • 3
  • 13
  • You may want to consider reading [this](http://developer.android.com/guide/practices/screens_support.html) – TronicZomB May 07 '13 at 14:16
  • Yeah, so I referenced that first - It tells you rough rules about pixels widths and heights, but these could vary in both size and in ratio, so getting the logic right is messy and perhaps not particularly future proof (I assume I'll also need to handle both orientations which is also a mess) That page doesn't mention anything on just being able to pull out a constant, which I would assume is something I can do. You know a quick "did you use the layout in this folder" rather than, "ok get me the pixels and I'll check that it's ok in both orientations etc..." – Imran May 07 '13 at 14:22

2 Answers2

2

In your code, use findViewById to get a reference to the View that exists only in your large screen layout. If that reference == null then you know you are working with your small screen layout otherwise you know you've gotten hold of the large screen version.

NigelK
  • 8,255
  • 2
  • 30
  • 28
1

If you really don't want to use Fragments for this, check out these links:
https://developer.android.com/reference/android/view/Display.html
https://developer.android.com/reference/android/util/DisplayMetrics.html

You could use the device's size like this:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (heightPixels > MIN_HEIGHT && widthPixels > MIN_WIDTH) {
  manipulate stuff...
}

EDIT:

Just saw your comment to the original question. To get the screen size category, this question seems to answer it:
How to determine device screen size category (small, normal, large, xlarge) using code?

Community
  • 1
  • 1
Matt Giles
  • 721
  • 5
  • 8