How to check your device on a tablet, mobile or Android TV in Android
I am making an application that will behave differently on different devices. Is there a way to check if my app is working on TV, mobile or tablet? Even I want to check that I am running my application on the emulator. I saw in some of the links that we can check the build number or similar things. I just want to make sure that this is the main thing that can tell us that the devices are different?
source to share
By definition, a tablet is 7 "or higher. The following is the test method:
/**
* Checks if the device is a tablet (7" or greater).
*/
private boolean checkIsTablet() {
Display display = ((Activity) this.mContext).getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
float widthInches = metrics.widthPixels / metrics.xdpi;
float heightInches = metrics.heightPixels / metrics.ydpi;
double diagonalInches = Math.sqrt(Math.pow(widthInches, 2) + Math.pow(heightInches, 2));
return diagonalInches >= 7.0;
}
And here's how to check if the device is Android TV:
/**
* Checks if the device is Android TV.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private boolean checkIsTelevision() {
int uiMode = mContext.getResources().getConfiguration().uiMode;
return (uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION;
}
Edit . As pointed out by the Redshirt user below, the above code snippet will only determine if the application is running in MODE_TYPE_TELEVISION. So, to specifically identify Android TV, you can also use this boolean check:
context.getPackageManager().hasSystemFeature("com.google.android.tv")
source to share
TV
First check if the device is a TV or not. This is how the documentation recommends :
public static final String TAG = "DeviceTypeRuntimeCheck";
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device")
} else {
Log.d(TAG, "Running on a non-TV Device")
}
Read more
Notes
- As the documentation discourages , don't use the same TV layouts as you do for phones and tablets.
Phone and tablet
Make different resource files to be used with different device sizes.
-
Telephone. This may be the default.
-
Tablet - use
sw600dp
orlarge
to define this.
See this answer for more details .
source to share