0

I would like to programmatically determine (on the Android platform), if the target device is a phone or a tablet. Is there a way to do this? I tried using Density Metrics to determine the resolution and used resources (images and layouts) accordingly but, it did not turn out well. There are differences when I launch the app on a phone (Droid X) and a tablet (Samsung Galaxy 10.1).

Please advise.

Anon
  • 1
  • 1
  • 1
  • What type of differences do you see? Look at this and realize just design for the display size and the hardware you need: http://groups.google.com/group/android-developers/browse_thread/thread/d6323d81f226f93f – James Black Aug 31 '11 at 00:06
  • Thank you for the reference, James. The differences were artifacts in rendering. The objects wouldn't render at the same position when testing on the phone as compared to the tablet. – Anon Aug 31 '11 at 18:06
  • So is the problem now solved? – James Black Sep 01 '11 at 00:13
  • I am experimenting a bit by adding more resources according to the screen size and density. For now, one of the modules is working using the alternative resource. – Anon Sep 02 '11 at 17:51
  • possible duplicate of [How to detect device is Android phone or Android tablet?](http://stackoverflow.com/questions/11330363/how-to-detect-device-is-android-phone-or-android-tablet) – Dharmendra Sep 23 '13 at 07:32

3 Answers3

1

You can use this code

private boolean isTabletDevice() {

if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
    // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
    Configuration con = getResources().getConfiguration();
    try {
        Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);
        Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
        return r;
    } catch (Exception x) {
        x.printStackTrace();
        return false;
    }
}
return false;
}

Link: http://www.androidsnippets.com/how-to-detect-tablet-device

Aracem
  • 7,227
  • 4
  • 35
  • 72
0

Based on Aracem's answer, I updated the snippet with normal tablet check for 3.2 or higher (sw600dp):

public static boolean isTablet(Context context) {
    try {
        if (android.os.Build.VERSION.SDK_INT >= 13) { // Honeycomb 3.2
            Configuration con = context.getResources().getConfiguration();
            Field fSmallestScreenWidthDp = con.getClass().getDeclaredField("smallestScreenWidthDp");
            return fSmallestScreenWidthDp.getInt(con) >= 600;
        } else if (android.os.Build.VERSION.SDK_INT >= 11) { // Honeycomb 3.0
            Configuration con = context.getResources().getConfiguration();
            Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast", int.class);
            Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
            return r;
        }
    } catch (Exception e) {
    }
    return false;

}
willy
  • 490
  • 4
  • 11
0

As James has already mentioned, You can determine screen size programatically and use a threshold Number to differrentiate between your logic.

mastDrinkNimbuPani
  • 1,249
  • 11
  • 14