How to detect device is Android phone or Android tablet?
Solution 1:
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.
Solution 2:
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");
}
Solution 3:
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;
}