31

My case is that the logic is same for both Phone and Tablet. But there is slight difference in the layout. And I tried with the following code

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

Samsung Tab 10" has the resolution of 1280 * 800 and S3 has the resolution of 1270 * 720. And this code returns the Size as XLarge for both the Tab and Phone as its criteria for checking is > 960 * 720.

I have tested inserting the respective UI in the layout folder in Res as Layout, Layout-Large and Layout-xLarge . But this didn't effect in anyway. while checking it took the UI from the Layout folder.

Anyway even though I place the UI in the different layout folders, I have to check them in the class file to set the respective ContentView.

Is there any other way to find it?

2

10 Answers 10

63

This subject is discussed in the Android Training:

http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali

Here is implementation,

Credit goes to ol_v_er for this simple and easy approach.

Some additional Information

You have now flag indicate whether your application is running on phone or tablet.

I have created two packages to handle UI and it's functionality,

com.phone com.tablet 

And you redirect control to your needed package

boolean tabletSize = getResources().getBoolean(R.bool.isTablet); if (tabletSize) { // do something //Start activity for tablet } else { // do something else //Start activity for phone } 

Refer

Note :I think for both 10 inch and 7 inch screen app take resources from res/values-sw600dp/ . But To be more specific I think for 10 inch tablet screen we can use res/values-sw720dp/

<resources> <bool name="isTablet">true</bool> </resources> 
Sign up to request clarification or add additional context in comments.

1 Comment

You should cite your source when you copy-paste: stackoverflow.com/a/9308284
18

Try this

 public boolean isTablet(Context context) { boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4); boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE); return (xlarge || large); } 

It will return true if you are using a tablet. It has been checked on Samsung Galaxy Tab 7" and Samsung Galaxy S3.

2 Comments

checking on two devices IS NOT a good enough check. Be very wary of this method as it is not reliable.
basing yourself on device dimension is the worst idea possible to determine a device. I don't know why this is the correct answer.
3

For example, you could set some res-values folder:

res/values-xlarge res/values-large res/values-sw600dp

etc. Then You could declare a boolean for each one:

 <resources> <bool name="isXLarge">true</bool> </resources> 

or

 <resources> <bool name="isLarge">true</bool> </resources> 

you can get the value by

 boolean xlargeValue = getResources().getBoolean(R.bool.isXlarge); boolean largevalue = getResources().getBoolean(R.bool.isLarge); boolean tabletValue = getResources().getBoolean(R.bool.sw620dp): 

Comments

3

Try this code your app is working device phone or tablet easy to fine call the method oncreate() inside

isTabletDevice(activity)

private static boolean isTabletDevice(Context activityContext) { boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) DisplayMetrics metrics = new DisplayMetrics(); Activity activity = (Activity) activityContext; activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); if (device_large) { //Tablet if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT){ return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){ return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_TV){ return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_HIGH){ return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_280){ return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) { return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_400) { return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XXHIGH) { return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_560) { return true; }else if(metrics.densityDpi == DisplayMetrics.DENSITY_XXXHIGH) { return true; } }else{ //Mobile } return false; } 

Comments

3

Old question, but this might help someone. If you want to find out if device is tablet (screen lager than 7"), or phone, you can use this util method:

Kotlin

fun isTablet(): Boolean { return App.instance.resources.configuration.smallestScreenWidthDp >= 600 } 

Java

public static Boolean isTablet(){ return App.instance.resources.configuration.smallestScreenWidthDp >= 600 } 

App.instance is aplication instance.

2 Comments

These values do not represent the physical size of the device. In multi-window mode, they will become smaller.
The Java code is mis-quoted. To start with, it is not Java code. To the person who posted it: if you want share any code which you didn't write, please understand it thoroughly, before rushing it out. I also agree with " Miloš Černilovský' comment: these values do not represent the physical size. The solution is definitely wrong in multi-window mode
2
public boolean isTablet() { try { // Compute screen size Context context = this; DisplayMetrics dm = context.getResources().getDisplayMetrics(); float screenWidth = dm.widthPixels / dm.xdpi; float screenHeight = dm.heightPixels / dm.ydpi; double size = Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2)); // Tablet devices have a screen size greater than 6 inches return size >= 6; } catch(Throwable t) { Log.e("Failed to compute screen size", t.toString()); return false; } 

}

Comments

1

Try this code. You can get the screen inches, On the basis of size you can get the tablet or android device

 String inputSystem; inputSystem = android.os.Build.ID; Log.d("hai",inputSystem); Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); // deprecated int height = display.getHeight(); // deprecated Log.d("hai",width+""); Log.d("hai",height+""); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); double x = Math.pow(width/dm.xdpi,2); double y = Math.pow(height/dm.ydpi,2); double screenInches = Math.sqrt(x+y); Log.d("hai","Screen inches : " + screenInches+""); 

2 Comments

Yes but if there are only these two devices ( Say samsung S3 and Samsung 10" Tablet) we can compare , but how do i do for other device also ....... there are various resolution sizes for various devices . S4 device screen is even more bigger .
but android device size is not like tablet size, so you can only get this one is android mobile device or tablet.
1

Use different resource files rather than trying to determine it programmatically. This will be enough for the majority of cases and is what the documentation recommends.

enter image description here

See my fuller answer here.

Comments

0

All other questions use resource qualifiers and methods, which do not represent the PHYSICAL size of the device, but the AVAILABLE screen size. For example, in the multi-window mode, the system will get resources from the "values" folder instead of "values-large", because the available screen size for the app became smaller. To determine, whether the physical device is a tablet or phone, use the following method (I use 640x480dp as the minimum size for a tablet, which is the definition of large devices, feel free to change these constants):

fun isTablet(context: Context): Boolean { val outSize = Point() val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager windowManager.defaultDisplay.getRealSize(outSize) outSize.x = pxToDp(windowManager, outSize.x) outSize.y = pxToDp(windowManager, outSize.y) val shorterSideDp: Int val longerSideDp: Int if (outSize.x > outSize.y) { shorterSideDp = outSize.y longerSideDp = outSize.x } else { shorterSideDp = outSize.x longerSideDp = outSize.y } return shorterSideDp > 480 && longerSideDp > 640 } 

Function for converting PX to DP:

@Dimension(unit = Dimension.DP) fun pxToDp(windowManager: WindowManager, @Dimension(unit = Dimension.PX) px: Int): Int { val displayMetrics = DisplayMetrics() windowManager.defaultDisplay.getRealMetrics(displayMetrics) return (px / displayMetrics.densityDpi.toFloat() * DisplayMetrics.DENSITY_DEFAULT).roundToInt() } 

Comments

-1

This is working perfectly well in my app:

private boolean isPhoneDevice(){ return getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY); } 

1 Comment

This only checks if the device can work as a telephone. Definition: "The device has a telephony radio with data communication support.". Some tablets and phones with really big screns will return true on this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.