Detect 7 Inch and 10 Inch Tablet Programmatically

Detect 7 inch and 10 inch tablet programmatically

You can use the DisplayMetrics to get a whole bunch of information about the screen that your app is running on.

First, we create a DisplayMetrics metrics object:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

From this, we can get the information required to size the display:

int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;

This will return the absolute value of the width and the height in pixels, so 1280x720 for the Galaxy SIII, the Galaxy Nexus etc.

This isn't usually helpful on its own, as when we're working on Android devices, we usually prefer to work in density independent pixels, dip.

You get the density of the screen using metrics again, in the form of a scale factor for the device, which is based on the Android Design Resources for mdpi, hdpi etc.
DPI scales

float scaleFactor = metrics.density;

From this result, we can calculate the amount of density independent pixels there are for a certain height or width.

float widthDp = widthPixels / scaleFactor
float heightDp = heightPixels / scaleFactor

The result you get from this will help you decide what type of screen you are working with in conjunction with the Android Configuration examples, which give you the relative dp for each screen size:

  • 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
  • 480dp: a tweener tablet like the Streak (480x800 mdpi).
  • 600dp: a 7” tablet (600x1024 mdpi).
  • 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

Using the above information, we know that if the smallest-width of the device is greater than 600dp, the device is a 7" tablet, if it's greater than 720dp, the device is a 10" tablet.

We can work out the smallest width using the min function of Math class, passing in the heightDp and the widthDp to return the smallestWidth.

float smallestWidth = Math.min(widthDp, heightDp);

if (smallestWidth > 720) {
//Device is a 10" tablet
}
else if (smallestWidth > 600) {
//Device is a 7" tablet
}

However, this doesn't always give you an exact match, especially when working with obscure tablets that might be misrepresenting their density as hdpi when it isn't, or that might only be 800 x 480 pixels yet still be on a 7" screen.

Further to these methods, if you ever need to know the exact dimensions of a device in inches, you can work that out too, using the metrics method for how many pixels there are per inch of the screen.

float widthDpi = metrics.xdpi;
float heightDpi = metrics.ydpi;

You can use the knowledge of how many pixels are in each inch of device and the amount of pixels in total to work out how many inches the device is.

float widthInches = widthPixels / widthDpi;
float heightInches = heightPixels / heightDpi;

This will return the height and width of the device in inches. This again isn't always that helpful for determining what type of device it is, as the advertised size of a device is the diagonal, all we have is the height and the width.

However, we also know that given the height of a triangle and the width, we can use the Pythagorean theorem to work out the length of the hypotenuse (In this case, the size of the screen diagonal).

//a² + b² = c²

//The size of the diagonal in inches is equal to the square root of the height in inches squared plus the width in inches squared.
double diagonalInches = Math.sqrt(
(widthInches * widthInches)
+ (heightInches * heightInches));

From this, we can work out whether the device is a tablet or not:

if (diagonalInches >= 10) {
//Device is a 10" tablet
}
else if (diagonalInches >= 7) {
//Device is a 7" tablet
}

And that's how you calculate what kind of device you're working with.

How to know 7 inch and 10 inch tablet programmatically without screen dimension calculations

use this logic put these value in respective folders like below

values-sw600dp//for seven inch

<bool name="isSmallerTablet">true</bool>

values-sw720dp//for other tablets.

<bool name="isSmallerTablet">false</bool>

and use it in activity like this

if(getResources().getBoolean(R.bool.isSmallerTablet)){

}

How make 10 inch tablet code compatible to 7 inch tablet in android

You can not direct assume that 10 inch tablet Layout will work same as on 7 inch Tablet .You have to make some changes to work on both like following from android Developer Site.

For other cases in which you want to further customize your UI to differentiate between sizes such as 7” and 10” tablets, you can define additional smallest width layouts:

res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)

Notice that the previous two sets of example resources use the "smallest width" qualifer, swdp, which specifies the smallest of the screen's two sides, regardless of the device's current orientation. Thus, using swdp is a simple way to specify the overall screen size available for your layout by ignoring the screen's orientation.

For Detail You can Refer Developer Site Link here

EDIT:

Please Refer this SO Link for Use of Drawable in all types Layout.Click here

Here is one more reference Guide for Understanding work with Multiple Screen In android,documents from Motorola Click here

Determine if the device is a smartphone or tablet?

This subject is discussed in the Android Training:

Use the Smallest-width Qualifier

If you read the entire topic, they explain how to set a boolean value in a specific value file (as res/values-sw600dp/attrs.xml):

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

Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:

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

Then, in the "standard" value file (as res/values/attrs.xml), you set the boolean to false:

<resources>
<bool name="isTablet">false</bool>
</resources>

Then in you activity, you can get this value and check if you are running in a tablet size device:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do something
} else {
// do something else
}

How to find Tablet or Phone in Android , Programmatically?

Try this

    public boolean isTablet(Context context) {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
return (xlarge || large);
}

It will return true if you are using a tablet. It has been checked on Samsung Galaxy Tab 7" and Samsung Galaxy S3.

How to detect 7 Android tablet in code

To calculate the height and width of the device (in inches) you can use the following code.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

float widthInInches = metrics.widthPixels / metrics.xdpi;
float heightInInches = metrics.heightPixels / metrics.ydpi;

Then the size can be calculated

double sizeInInches = Math.sqrt(Math.pow(widthInInches, 2) + Math.pow(heightInInches, 2));
//0.5" buffer for 7" devices
boolean is7inchTablet = sizeInInches >= 6.5 && sizeInInches <= 7.5;

As per Commonsware suggestion above, a potentially faster, but less obvious implementation.

double sizeInInchesSquared = (widthInInches * widthInInches) + (heightInInches * heightInInches);
//0.5" buffer for 7" devices (6.5^2 = 42.25) (7.5^2 = 56.25)
boolean is7inchTablet = sizeInInchesSquared >= 42.25 && sizeInInchesSquared <= 56.25;

How to get android device screen size?

try this code to get screen size in inch

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
double wi=(double)width/(double)dm.xdpi;
double hi=(double)height/(double)dm.ydpi;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double screenInches = Math.sqrt(x+y);


Related Topics



Leave a reply



Submit