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
.apkfor Android TV, using Gradle flavors. (This is a good idea anyway since it will keep down the.apksize.) - Do not use the
televisionresource qualifier, put TV resources in unqualified resource directories instead. (When building a separate.apkfor TV only, this resource qualifier is not necessary.) - Specify both
android.intent.category.LAUNCHERandandroid.intent.category.LEANBACK_LAUNCHERin 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:iconandandroid:bannerin 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
onCreateon 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);
}