-1

Hello I have created an application, in that I want to detect Tablet or Mobile at startup.

I have 5.5" Tablet.

How to make it possible ?

Sagar Zala
  • 45
  • 1
  • 8

2 Answers2

1

try this

 public static boolean isTabletDevice(Context activityContext) {
    // Verifies if the Generalized Size of the device is XLARGE to be
    // considered a Tablet
    boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & 
                        Configuration.SCREENLAYOUT_SIZE_MASK) == 
                        Configuration.SCREENLAYOUT_SIZE_XLARGE);

    // If XLarge, checks if the Generalized Density is at least MDPI
    // (160dpi)
    if (xlarge) {
        DisplayMetrics metrics = new DisplayMetrics();
        Activity activity = (Activity) activityContext;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
        // DENSITY_TV=213, DENSITY_XHIGH=320
        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {

            // Yes, this is a tablet!
            return true;
        }
    }

    // No, this is not a tablet!
    return false;
}
0

In android there are directories like values-normal-ldpi which access dimensions for phones, values-sw600dp for 7 inch tablet and values-sw720dp for 10 inch tablet. You can take a Boolean variable in those directories like :-

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

set it true for both tablet directories and false for phone. access it like

    context.getResources().getBoolean(R.bool.is_tablet);

If it returns true it's tablet else phone.

Amrut
  • 2,655
  • 3
  • 24
  • 44
  • My tablet's resolution 480*800 (Model Number-UBISLATE 7C+) and mobile resolution is also 480*800, so get confusion. – Sagar Zala Feb 24 '14 at 06:08
  • @SagarZala I think above code will work for your Scenario. see this http://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet. – Amrut Feb 24 '14 at 06:13
  • In above link, what is SharedCode (SharedCode.width) ? – Sagar Zala Feb 24 '14 at 06:19
  • @SagarZala See the answer given by user ol_v_er. I don't know what is SharedCode.width. – Amrut Feb 24 '14 at 06:21