How to determine device screen size category (small, normal, large, xlarge) using code?
You can use the Configuration.screenLayout
bitmask.
Example:
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE) {
// on a large screen device ...
}
How to determine device screen size category (small, normal, large, xlarge) in order to test different screens in an application?
Do not confuse screen size and density.
For example, a device can have normal screen but high density (hdp)(WVGA800 (480x800)).
(getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
here you are getting the screen size.
You have to use this code to determine density:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density==DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XHIGH) {
Toast.makeText(this, "DENSITY_XHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XXHIGH) {
Toast.makeText(this, "DENSITY_XXHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XXXHIGH) {
Toast.makeText(this, "DENSITY_XXXHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
https://developer.android.com/about/dashboards/index.html
How big is small, normal, large and xlarge?
In my experience, there are no "specific limits" to the screen sizes using the small, normal, large, x-large notation. They are more general and will get you by in the majority of cases. These values are part of the rom on the device, so it's up to the manufacturer to conform to the standards in the API documentation or not.
If you are targeting API 13+, you can use the sw-dp notation to get more specific (see http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts).
How to get what layout i'm using normal , large or xlarge
What exactly are you trying to do? It sounds like you want different images for the same button depending on the layout, which is already supported without having to explicitly know the screen size you're on. You should be able to use qualifiers like drawable-xhdpi, drawable-sw600dp-xhdpi and drawable-sw800dp-xhdpi. If you put the button image in each of those with the same name, give the button that drawable and the system will choose the correct drawable at run-time without needing to explicitly know the size.
If there's some logic in your onClick that needs to know the screen size, that's a different issue, and if you give more detail about what you want to do there, there might be a similar way that the framework already supports it.
How do I get the ScreenSize programmatically in android
Copy and paste this code into your Activity
and when it is executed it will Toast
the device's screen size category.
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
String toastMsg;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
toastMsg = "Large screen";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
toastMsg = "Small screen";
break;
default:
toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
Get screen size bucket programmatically?
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.
Related Topics
Error:(6, 0) Gradle Dsl Method Not Found: 'Google()'
Get Content Uri from File Path in Android
How to Set Android Camera Orientation Properly
How to Implement Drawerarrowtoggle from Android Appcompat V7 21 Library
How to Change the Radio Button Icon in an Android Radio Button Group
How to Parse JSON String in Android
Select Multiple Images from Android Gallery
How to Make Toolbar Transparent
Image in Canvas with Touch Events
Calculate Compass Bearing/Heading to Location in Android
How to Implement Recyclerview with Cardview Rows in a Fragment with Tablayout
How to Make a Dotted/Dashed Line in Android
How to Implement a Long Click Listener on a Listview
Creating a Button in Android Toolbar
Android - Storing/Retrieving Strings with Shared Preferences