I am making an application which will behave differently on different devices. Is there any way to check that my application is running on TV Device, Mobile or Tablet? Even I want to check I am running my application on Emulator. In some links I have seen we can check build number or things like these. I just want to make sure which is the main thing which can let us know that devices are different?
Asked
Active
Viewed 1.2k times
6
-
1Check this one http://stackoverflow.com/questions/5832368/tablet-or-phone-android – barisemreefe Dec 26 '14 at 13:38
-
1Android does not think in terms of "phone" versus "tablet". There are only device features (e.g., does the device have telephony?), screen sizes, and the like. You need to decide more specifically what characteristics of the device is that you care about, rather than thinking in "phone", "tablet" shorthand. – CommonsWare Dec 26 '14 at 13:44
2 Answers
8
By definition, a tablet is 7" or greater. Here is a method to check for it:
/**
* 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 is how to check whether 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 Redshirt user below, the above code snippet will only detect whether the app is running on a MODE_TYPE_TELEVISION. So to detect Android TV specifically, you can use this boolean check too:
context.getPackageManager().hasSystemFeature("com.google.android.tv")

Naofumi
- 331
- 4
- 9

IgorGanapolsky
- 26,189
- 23
- 116
- 147
-
That second code example does not determine if the device is Android TV, it just determines if the device is running in TV mode. So the device could end up being an Amazon Fire TV or any of the myriad of other set top devices but not be a true Android TV device. By true Android TV I'm referring to something that has the Leanback homescreen and handles recommendations. – Redshirt Jan 07 '16 at 01:43
-
@Redshirt Amazon Fire TV uses Leanback as well as recommendations! – IgorGanapolsky Jan 07 '16 at 15:59
-
2Fire TV can display leanback activities if you add the appropriate library to your build.gradle, but it does NOT use the leanback launcher. The comments above say it's testing for Android TV which is not what the method is actually doing. The following code is how you can test for Android TV. It will return false for a Fire TV but will return true for an actual Android TV device such as the nvidia shield. `getPackageManager().hasSystemFeature("android.software.leanback")` – Redshirt Jan 07 '16 at 17:36
4
TV
First check if the device is a TV or not. Here is how the documentation recommends doing it:
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")
}
More reading
Notes
- As discouraged in the documentation, don't use the exact same layouts for TV as for phones and tablets.
Phone and Tablet
Make different resource files to be used with the various device sizes.
Phone - This can be the default.
Tablet - Use
sw600dp
orlarge
to determine this.
See this answer for more information about this.
-
Is TV not counted as `large` screen device? Looks like check for tablet will also be valid for TV, so we can't determine right device type. Am I wrong? – Nikita Kalugin May 18 '22 at 10:59
-
1@NikitaKalugin, If you want to use the same UI layout on a TV as on a tablet then maybe that would work. I'm not sure. I haven't tried it. I would think if you are developing for TV, though, that you would probably want to modify some things about the UI. – Suragch May 20 '22 at 00:42