Home Java Introduction to Android TV Application Development

Introduction to Android TV Application Development

by admin

This article will introduce you to the development of a simple application for Android TV. It is primarily aimed at those who already have some experience in Android app development, so I will not explain here what is Activity, Fragments, etc.
There is a lot of Russian-language information on developing applications for Android and not a few articles on writing HelloWord for the same. Interested in developing applications for Android TV, I immediately began to explore this topic on English-language sites. To my surprise it turned out that there was not much material, so I decided to see what was available in Russian. In Russian did not find anything (probably badly searched). So I intend to correct this situation.
Since the application interface for phones and Android TV has significant differences, we should create an application interface that is suitable for interaction on TV. For example, we should create applications with which we can interact using only keys – ↑ ↓ → ←. The LeanbackSupport library can help us in implementing such an interface, as it allows us to easily create a UI that will be convenient for working with applications on Android TV.

Creating a project in Android Studio

Launch Android Studio and create a new project. When creating, select the TV platform and specify the minimum version of the SDK. Android Studio will prompt us to create an "Android TV Activity", but at this point we should select "Add No Activity" because if you choose to create an Activity, the AS will create many classes and files that are initially harder to understand.

Creating Activity

First we need to create a new XML file called activity_main.xml which will contain the markup for our Activity. We’ll change the markup code later.
Now we need to create a class which inherits from Activity. To do this, create a new class named MainActivity and inherit it from the Activty class. Predefine the onCreate(Bundle SIS) method and set the content for the Activity from the created markup file in it.

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

Announcement of Activity in the application manifest file

If you try to start the app at this point, it won’t start of course, because we don’t have any Activity declared in the AndroidManifest.xml file.
You need to add the following code to the manifest file.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.simpletvapp"><uses-featureandroid:name="android.hardware.touchscreen"android:required="false" /><uses-featureandroid:name="android.software.leanback"android:required="true" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/Theme.Leanback"><activityandroid:name=".MainActivity"android:icon="@drawable/app_icon_your_company"android:label="@string/app_name"android:logo="@drawable/app_icon_your_company" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LEANBACK_LAUNCHER" /></intent-filter></activity></application></manifest>

Now let’s look at a few things. The part of the code below disables the touch.

<uses-featureandroid:name="android.hardware.touchscreen"android:required="false" />

In this part you specify that the app should only run on Android TV. If you are developing the app for something other than TV, you should set it to false.

<uses-featureandroid:name="android.software.leanback"android:required="true" />

When declaring an Activity, we specify in the intent-filter that the Activity must run on the Android TV.

<category android:name=”android.intent.category.LEANBACK_LAUNCHER/>

Creating a fragment

Now we need to create a Java class named MainFragment and inherit from the BrowseFragment class from the LeanbackSupport library. BrowseFragment allows you to create a standard Android TV application interface.
Now we can link the created fragment to our Activity, to do that we need to put the following markup code into the Activity markup file (in my case it’s activity_main.xml).

<?xml version="1.0" encoding="utf-8"?><fragment xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/main_browse_fragment"android:name="com.simpletvapp.MainFragment"android:layout_width="match_parent"android:layout_height="match_parent" />

Running the application

In order to run the application, you need to create an Android TV emulator. This can be created in the Android Virtual Device Manager.
After creating the emulator you can run our application on it. At the moment it looks like this.
Introduction to Android TV Application Development
Here you see an empty BrowseFragment. You can see the RowsFragment on the left side of the application (the fragment is responsible for displaying the header list) and the HeaderFragment on the right side of the screen (responsible for displaying the header content).
Next we’ll fill in the HeaderFragment, RowsFragment and take a closer look at them. Before that, let’s set the main UI colors and header for the application.

Configuring the application style

Here I added the setupUI() method to MainFragment.java and called it in the predefined onActivityCreated method.

@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);setupUI();}private void setupUI() {setTitle("Hello Android TV!");int color = ContextCompat.getColor(getActivity(), R.color.fastlane_background);setBrandColor(color);}

If you run the application at this point, you should see a UI like this.
Introduction to Android TV Application Development

Filling BrowseFragment

Let’s first understand the contents of the BrowseFragment using an example of a completed Android TV application. Each header in the left side of the fragment (HeaderItem) has a row with the content in the right side. The combination "header (HeaderItem) + content in the right part of the application", contains the ListRow class. The BrowseFragment content is a list from the ListRow. The HeaderItem and the list with content in the right part have a one-to-one relationship.
Introduction to Android TV Application Development
Let’s take a closer look at ListRow. The ArrayObjectAdapter is responsible for the list with content. In this case CardInfo is a content item. CardInfo can be any object. How to create and display CardInfo we’ll see later.
Introduction to Android TV Application Development
We can now draw the following conclusions :
ArrayObjectAdapter – responsible for list from ListRow
ListRow = HeaderItem (header) + ArrayObjectAdapter (content list on the right side)

Presenter Class

Filling of content items is defined using the Presenter class from the LeanbackSupport library. It defines the display of a content item. Presenter is an abstract class, so we have to create a new class and inherit it from Presenter. When you create a new class you must predefine at least 3 methods :

onCreateViewHolder(ViewGroup parent);onBindViewHolder(ViewGolder, viewHolder, Object item);onUnbindViewHolder(ViewHolder viewHolder);

Presenter contains an internal class ViewHolder that allows us to refer to the View (content element). We can access the View through the ViewHolder in specific events, for example in the methods of the Presenter class onBind() or onUnbind()

Filling HeadersFragment and RowsFragment

Let’s get down to business. Here we create a GridItemPresenter class inherited from the Presenter class. In this application Object (content item) displays a string and ViewHolder contains a TextView to display this string. The View is created in the onCreateViewHolder() method, and its filling is done in the onBindViewHolder() method.

public class GridItemPresenter extends Presenter {private static final int WIDTH_ITEM = 300;private static final int HEIGHT_ITEM = 200;@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent) {TextView view = new TextView(parent.getContext());view.setLayoutParams(new ViewGroup.LayoutParams(WIDTH_ITEM, HEIGHT_ITEM));view.setFocusable(true);view.setFocusableInTouchMode(true);view.setGravity(Gravity.CENTER);view.setBackgroundColor(ContextCompat.getColor(parent.getContext(), R.color.default_background));view.setTextColor(Color.WHITE);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(ViewHolder viewHolder, Object item) {TextView textView = (TextView) viewHolder.view;String str = (String) item;textView.setText(str);}@Overridepublic void onUnbindViewHolder(ViewHolder viewHolder) {}}

We add the loadRows() method to the MainFragment class and call it in the predefined onActivityCreated() method

private void loadRows() {// adapter responsible for ListRows (ListRow = header + content)ArrayObjectAdapter rowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());// the class is responsible for the header, we specify in the constructor that this is the first header in the list, // and the header itself contains the text "Header 1"HeaderItem headerItem = new HeaderItem(0, "Header 1");// our class which is responsible for filling the content itemsGridItemPresenter itemPresenter = new GridItemPresenter();// our adapter, in charge of displaying the content on the right sideArrayObjectAdapter gridAdapter = new ArrayObjectAdapter(itemPresenter);// add three content itemsgridAdapter.add(0, "Item 1");gridAdapter.add(1, "Item 2");gridAdapter.add(2, "Element 3");// add a ListRow to the adapter responsible for ListRows.// into the constructor pass the class responsible for the header and the adapter responsible for// display the list of contentrowsAdapter.add(new ListRow(headerItem, gridAdapter));setAdapter(rowsAdapter);}

Now you can run the application and see the screen below.
Introduction to Android TV Application Development
That’s all for now. In this article, I tried to explain some of the basic principles of creating an Android TV app.
This article was based on this and this one manuals.

You may also like