I want to determine whether the current device has a small, medium, large or xlarge screen in code. I can't find anything in the SDK docs that would help me get to that information. All the methods / classes I have looked at provide only absolute values (i.e. screen size in pixels, screen density, etc.).
Is there a way to tell what kind of screen I'm running on in code?
Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.
You can get info on the display from the DisplayMetrics struct: DisplayMetrics metrics = getResources(). getDisplayMetrics(); Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales to the actual screen size.
I ended up using bool
resources placed in the different bucket folders. I only needed to differentiate between normal (small / medium) and large (large / xlarge) screens, so I did this:
values/bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="screen_large">false</bool>
</resources>
values-large/bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="screen_large">true</bool>
</resources>
Then, I just call getBoolean(R.bool.screen_large)
to tell whether the screen is large or not. This way it's 100% up to the platform decide what screen the device has.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With