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 :
- Tv Launcher Activity: An app intended to run on Tv must define an activity inside manifest.
- 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>
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 :
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shifali Rai
Android developer: Worked on Android mobile, Android Tv and Fire Tv