This project demonstrates the steps to be taken to port/create an app for Android TV. Compile this project as is and install it on to the TV. You can see that the app is not listed in the Home Screen app drawer. You can only open it from Settings -> Apps -> Downloaded Apps -> Android TvDemo. Experience its UI. You will find that the focus is not clear and navigation may not be as per your expectation.
Now start following the below steps to make it TV ready.
-
Modify the Android Manifest file to add the leanback launcher intent to your activity. Without this, your app won't appear in the Home screen nor in the TV Play Store.
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter>
-
Once you have added the intent to your manifest, you need to add a Home Banner of 320x180 px. Home Banner should carry the name of your application. Specify the reference to this banner in your Manifest under your activity: android:banner="@mipmap/home_tv"
Now you should be able to see and launch your app from the Home Screen. Without the first 2 steps, your app wont be listed in the Home screen, app drawer.
- Now coming to the layout of your application. It will look like this:
As you can see, it is difficult to identify from a distance, which is the currently focused button.
- So, you either need to add the leanback effect (the focused element will zoom out a bit) or draw a border around it or paint it with a different color. I have chosen the 3rd approach. This is how it looks now:
You can see that the 'Left' button is highlighted in Red. To achieve this effect, I have applied the style style="@style/button_custom" to all the buttons. See /app/src/main/res/layout/activity_main.xml
-
To handle D-Pad navigation:
a. First you have to decide which view gets the focus on launch. You have to request it using view.requestFocus().
b. Now once you set the focus, you have to control the navigation. If you press 'up' button of your remote, from the 'Bottom' button, the focus first goes to 'Right' button, then to the 'Top' button. In case, you want the focus to reach the 'Top' button directly from 'Bottom' button, you need to set buttonbottom.setNextFocusUpId(R.id.buttontop); Similarly, you can modify for the other views.
With these changes, your basic TV app is ready.
Once the basic TV app is ready, you can enhance it by having your mobile exchange data and controls with the TV. This can be done using NSD (generic) and Cast sdk. This project already has the files related to NSD already added in the folder /app/src/main/java/com/android/tv/androidtvapp/nsd.
You can follow the next project https://github.com/nikhiltiru/tv-mobilecontrol to make these enhancements.