Tv Support to an already existing Android App

Posted By : Shifali Rai | 27-Dec-2018

Adding Tv support to Android app gives user immense comfort and opportunities as the apps are customized to run on a large screen and can be accessed while sitting on a couch.TV apps use the same interface and programming structure as of phones so one can provide Tv support to their already existing app.

 

Prerequisites :

  1.  Tv Launcher Activity: An app intended to run on Tv must define an activity inside manifest. 
  2.  Tv Support Libraries: Tv support libraries are optional. They provide special widgets for Tv interface.

 

Declare Tv activity 

Tv launcher activity must be defined in your apps manifest using CATEGORY_LEANBACK_LAUNCHER inside the intent filter.The following code piece shows a way to embody this intent filter :

<activity android:name=".ui.activity.SplashActivityTv"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
        </activity>

 

 Leanback Support 

Add the following code inside your manifest tag in app manifest:

<manifest>
    <uses-feature android:name="android.software.leanback"
        android:required="false" />
    ...
</manifest>

 

Manage Tv Device 

If the user wants the interface to work differently on phone and Tv one can check for the device on which the app is running. Below code piece shows a way to do so:

UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
        if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
            Log.d(TAGG, "Running on a TV Device");
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            Log.d(TAGG, "Running on a non-TV Device");
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

 

Tv Layouts

Tv layouts should follow some basic guidelines to have an effective appearance.Follow the below tips :

  • Build layouts in landscape mode.
  • Create UIs using Fragments and views such as GridView to utilize all the horizontal space.
  • Use View groups such as LinearLayout or Relative Layout.

 

Accessing Tv Buttons

One can access  Tv buttons and to perform some customized actions on it. Below is an example of performing an action on  a key :

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {

    switch (keyCode) {
        case KeyEvent.KEYCODE_A:
        {
            //your Action code
            return true;
        }
    }
    return super.onKeyDown(keyCode, event);
}

 

Conclusion

This blog helps in providing basic TV support to your Android app. For more on Tv support follow the links below :

  1. https://android-developers.googleblog.com/2016/11/adding-tv-channels-to-your-app-with-the-tif-companion-library.html
  2. https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_CHANNEL_UP

About Author

Author Image
Shifali Rai

Android developer: Worked on Android mobile, Android Tv and Fire Tv

Request for Proposal

Name is required

Comment is required

Sending message..