This question already has an answer here:
-
Tablet or Phone - Android
29 answers
I would like to get to detect whether the given device is a tablet or phone in android.I have tried the two in the simulator but none worked. Both are here:
First
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
{
//code
}
Second
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;
}
Try this code. You can get the screen inches
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+"");
create two layout folders in res folder like this layout and layout_xlarge.Create one unwanted view in layout-xlarge file.Get this unwanted view id programatically.Access that unwanted id in code within try catch blocak if you get null pointer exception then it is small device otherwise it is tablet.one more thing hide the unwanted view in layout-xlarge screen.
I think the first question to answer is what you mean by "phone" or "tablet". We may have some distinction in our minds, but in reality, your application should not care. The Asus Transformer is a tablet, the Samsung Galaxy Nexus is a phone. What is the Samsung Galaxy Note? Engadget and others call it a "phablet" -- what should your application assume in that case?
Is your definition of "tablet" any device that does not have a SIM card? That is also a faulty definition because there are many tablets that do have a built in SIM card. Is your definition of "tablet" any device that defaults to landscape mode and rotates to portrait mode? Perhaps that is one approach but I doubt that is sufficient since there could be tablets that default to portrait, or worse, have square form factors in future.
In my opinion, the best way to handle this is to use Android's guidelines and switch on UI layouts. If you think you have a clear definition of tablets versus phones and absolutely need to detect one over the other, you probably need to do something like extract the model name from the browser's user agent string and then match it against a database of names of tablets that you maintain. I know. Yuck.
place this method in onResume() and can check.
public double tabletSize() {
double size = 0;
try {
// Compute screen size
DisplayMetrics dm = context.getResources().getDisplayMetrics();
float screenWidth = dm.widthPixels / dm.xdpi;
float screenHeight = dm.heightPixels / dm.ydpi;
size = Math.sqrt(Math.pow(screenWidth, 2) +
Math.pow(screenHeight, 2));
} catch(Throwable t) {
}
return size;
}
generally tablets starts after 6 inch size.