Support fake Android TV devices
By Mikael Ståldal
Android TV is included in some modern Smart TVs, and there are also external set-top-boxes to get Android TV for any TV with HDMI input.
However, there are also some set-top-boxes which almost, but not quite, support Android TV.
With not so much extra effort, it is possible to make an Android app supporting those fake Android TV devices as well as the real ones.
In addition to the guidelines from Google for building Android TV apps, do this to support the fake TV Android TV devices as well:
- If you build an app for both Android TV and mobile (smartphone/tablet), make sure to build a separate
.apk
for Android TV, using Gradle flavors. (This is a good idea anyway since it will keep down the.apk
size.) - Do not use the
television
resource qualifier, put TV resources in unqualified resource directories instead. (When building a separate.apk
for TV only, this resource qualifier is not necessary.) - Specify both
android.intent.category.LAUNCHER
andandroid.intent.category.LEANBACK_LAUNCHER
in the intent-filter for the launcher activity in the app manifest.:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
- Specify Leanback support with required=false in the app manifest:
<uses-feature
android:name="android.software.leanback"
android:required="false" />
- Specify both
android:icon
andandroid:banner
in the app manifest:
<application
android:banner="@drawable/my_banner"
android:icon="@drawable/my_icon">
- If you use the Leanback SearchFragment, you need to disable voice search on the fake devices, since they usually does not support speech recognition:
static boolean isRealAndroidTvDevice(Context context) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(UI_MODE_SERVICE);
return (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION);
}
if (!isRealAndroidTvDevice(context)) {
setSpeechRecognitionCallback(new SpeechRecognitionCallback() {
@Override
public void recognizeSpeech() {
// do nothing
}
});
}
- The fake Android TV devices usually get the display pixel density wrong. You can programatically fix this in
onCreate
on the launcher activity:
if (!isRealAndroidTvDevice(context)) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.density = 2.0f;
metrics.densityDpi = 320;
metrics.scaledDensity = 2.0f;
getResources().getDisplayMetrics().setTo(metrics);
}