Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen size bucket programmatically?

Tags:

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?

like image 871
Felix Avatar asked Sep 02 '11 13:09

Felix


People also ask

How can get screen width and height in android programmatically?

Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.

How do you measure screen density?

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.


1 Answers

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.

like image 55
Felix Avatar answered Oct 06 '22 23:10

Felix