How to detect device is Android phone or Android tablet?

China☆狼群 提交于 2019-11-26 15:11:41

Use this:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

which will return true if the device is operating on a large screen.

Some other helpful methods can be found here.

You can also try this
Add boolean parameter in resource file.
in res/values/dimen.xml file add this line

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

while in res/values-sw600dp/dimen.xml file add this line:

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

then in your java file get this value:

if(getResources().getBoolean(R.bool.isTab)) {
    System.out.println("tablet");
} else {
    System.out.println("mobile");
}

This code snippet will tell whether device type is 7" Inch or more and Mdpi or higher resolution. You can change the implementation as per your need.

 private static boolean isTabletDevice(Context activityContext) {
        boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_LARGE);

        if (device_large) {
            DisplayMetrics metrics = new DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            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) {
                AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");
                return true;
            }
        }
        AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");
        return false;
    }

Use Google Play store capabilities and only enable to download your tablet app on tablets and the phone app on phones.

If the user then installs the wrong app then they must have installed using another method.

user3703142

We had the similar issue with our app that should switch based on the device type - Tab/Phone. IOS gave us the device type perfectly but the same idea wasn't working with Android. the resolution/ DPI method failed with small res tabs, high res phones. after a lot of torn hairs and banging our heads over the wall, we tried a weird idea and it worked exceptionally well that won't depend on the resolutions. this should help you too.

in the Main class, write this and you should get your device type as null for TAB and mobile for Phone.

String ua=new WebView(this).getSettings().getUserAgentString();


if(ua.contains("Mobile")){
   //Your code for Mobile
}else{
   //Your code for TAB            
}
Vaishali Sutariya

Use following code to identify device type.

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");
              Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
              return r;
         } catch (Exception x) {
              return false;
         }
      }
    return false;
}

I think this should detect if something is able to make a phonecall, everything else would be a tablet/TV without phone capabilities.

As far as I have seen this is the only thing not relying on screensize.

public static boolean isTelephone(){
    //pseudocode, don't have the copy and paste here right know and can't remember every line
    return new Intent(ACTION_DIAL).resolveActivity() != null;
}

If you want to decide device is tablet or phone based on screen inch, you can use following

device 6.5 inches or higher consider as tablet, but some recent handheld phone has higher diagonal value. good thing with following solution you can set the margin.

public boolean isDeviceTablet(){
    DisplayMetrics metrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float yInches= metrics.heightPixels/metrics.ydpi;
    float xInches= metrics.widthPixels/metrics.xdpi;
    double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);
    if (diagonalInches>=6.5) {
        return true;
    }
    return false;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!