2

I am trying to detect 7" tablets in my code (i.e. Kindle Fire & Nook Color). However, simply testing for minimum dimensions 1024x600 is not good, because then the Galaxy Nexus would pass this test as well. Anybody has experience with detecting this kind of information?

Thanks, Igor

EDIT: I have found one way to detect Kindle Fire & Nook Color devices with the following code:

Activity activity = (Activity) activityContext;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
if ((width == 1024 && height == 600) || (width == 600 && height == 1024)) {    
        //Detects 7" tablets: i.e. Kindle Fire & Nook Color
        isTablet = true;
    }
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • Your edited code will claim the Galaxy Nexus is a tablet. – CommonsWare May 23 '12 at 17:01
  • @CommonsWare I have tested the code and can unequivocally say that Galaxy Nexus will NOT evaluate to True. Can your tests prove otherwise? – IgorGanapolsky May 23 '12 at 19:44
  • Sorry, I was basing that on your comment above that the Galaxy Nexus was 1024x600 (it is actually 1280x720). More importantly, though, you are simply looking for 1024x600 displays, which says **nothing about size**. It happens to match some 7" tablets. It might not match other 7" tablets that run other resolutions. It might match xhdpi phones that run that resolution. – CommonsWare May 23 '12 at 21:45
  • @CommonsWare If you use the application - ScreenInfo - from Google Play, you can see all the information about your device's size. Mine tells me that my Galaxy Nexus is 720px x 1184px, and 592dp x 360dp. So obviously it would not be detected by my code above. And what is your solution for adequately detecting 7" tablets? I gather Justin's solution below is a good one. – IgorGanapolsky May 23 '12 at 21:50
  • 1
    ScreenInfo is subtracting out something (status or system bar), as the device is 1280x720. Justin's first code listing is fine; the second code listing works, though the purist in me would not use `pow()` (multiplication has a chance to be faster) or `sqrt()` (just compare against the squares of the values). – CommonsWare May 23 '12 at 22:06

4 Answers4

5

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; 
Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • Ok, on the Galaxy Nexus widthInInches becomes 2.2834647 & heightInInches is 3.714567. For Kindle fire they are 3.644808 & 6.1816273. Seems like a good solution. Thanks. – IgorGanapolsky May 23 '12 at 17:09
  • 1
    Sounds right to me. Using Pythagorean theorem to calculate the diagonal, we get about 7.17 inches for the kindle fire. If you use a half inch error buffer (6.5" to 7.5") you should be able to get all 7" tablets pretty easily. – Justin Breitfeller May 23 '12 at 17:16
  • You should also take into account that sizeInInches will return slightly different numbers depending on screen orientation. For example, Asus Transformer Pad TF300 returns these measurements: 9.18(portrait), 9.27(landscape). Samsung GN returns: 4.36(port) 4.41(land). – IgorGanapolsky May 25 '12 at 00:28
  • 1
    xdpi and ydpi cannot be trusted to be set correctly by manufactures - sometimes they aren't set at all, other times (Samsung Galaxy Tab 1) they lie to fake their resolution. – FunkTheMonk Nov 22 '12 at 17:57
1

If it's the tablet you care about rather than dimensions, specifically: there is the Build class, which can tell you things like model and product.

Turnsole
  • 3,422
  • 5
  • 30
  • 52
1

See the following. Seems to not work on all devices. If the two tablets are the only devices you are worried about, I'd give either implementation a try.

https://stackoverflow.com/a/10080537/300972

https://groups.google.com/group/android-developers/browse_thread/thread/cae5ff90157098b1?pli=1

Community
  • 1
  • 1
Oh Danny Boy
  • 4,857
  • 8
  • 56
  • 88
1

I know it's old thread but what about this way of:

public boolean isLargeScreen() {

    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.device_screen, null);
    FrameLayout menu = (FrameLayout) view.findViewById(R.id.menu);


    if (left_menu != null) {
        return true;
    } else {
        return false;
    }
}

then:

if ( isLargeScreen() ) {
    // do something for large screen & extra large screen
} else {
    // do something for normal screen size
}

and xml layouts (for example):

res/layout/device_screen.xml // layout for normal screen size ("default")

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    [leave it blank]

</LinearLayout>

res/layout-large/device_screen.xml // layout for large screen size tablet 7'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/menu"
        ... />

</LinearLayout>

res/layout-xlarge/device_screen.xml // layout for extra large screen size > tablet 10'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/menu"
        ... />

</LinearLayout>

Tested on HTC Desire & Samsung Galaxy Note 10.1 - works great!

kst
  • 51
  • 1
  • 4