Channels on the home screen

The Android TV home screen, or simply the home screen, provides a UI that
displays recommended content as a table of channels and programs. Each row is a channel. A channel contains cards for every program available on that channel:

This document demonstrates how to add channels and programs to the home screen, update content, handle user actions, and provide the best experience for your users. (If you'd like to dig deeper into the API, try the
home screen codelab
and watch the I/O 2017 Android TV session.)

Note: Recommendations channels are only available in
Android 8.0 (API level 26) and later. You must use them to supply
recommendations for apps running in Android 8.0 (API level 26) and later. To
supply recommendations for apps running on earlier versions of Android, your app
must use the
recommendations row
instead.

The home screen UI

Apps can create new channels, add, remove, and update the programs in a channel, and control the order of programs in a channel.
For example an app can create a channel called "What's New" and show cards for newly available programs.

Apps cannot control the order in which channels appear in the home screen. When your app creates a new channel, the home screen adds it to the bottom of the channel list. The user can reorder, hide, and show channels.

The Watch Next channel

The Watch Next channel is the second row that appears in the home screen, after
the apps row. The system creates and maintains this channel. Your app can add
programs to the Watch Next channel: programs that the user marked as
interesting, stopped watching in the middle, or that are related to the content
the user is watching (like the next episode in a series or next season of a
show).

The Watch Next channel has some constraints: Your app cannot move, remove, or
hide the Watch Next channel's row.

Note: On the home screen, the Watch Next channel has the label "Play Next".
However, the Android classes used to manage the Watch Next channel are
WatchNextProgram and
WatchNextPrograms.
They have methods and constants with the stem "watchnext".

App channels

The channels that your app creates all follow this life cycle:

User discovers a channel in your app and requests to add it to the home screen.

App creates the channel and adds it to the TvProvider (at this point the channel is not visible).

App asks the system to display the channel.

System asks user to approve the new channel.

New channel appears in the last row of the home screen.

The default channel

Your app can offer any number of channels for the user to add to the home screen. The user usually has to
select and approve each channel before it appears in the home screen. Every app has the option of creating one default channel.
The default channel is special because it automatically appears in the home screen; the user does not have to
explicitly request it.

Prerequisites

The Android TV home screen uses Android's TvProvider APIs to manage the channels and programs that your app creates.
To access the provider's data, add the following permission to your app's manifest:

Channels

The first channel your app creates becomes its default channel. The default channel automatically appears in the home screen. All other channels you create must be selected and accepted by the user before they can appear in the home screen.

Creating a channel

Your app should ask the system to show newly added channels only when it is running in the foreground. This prevents your app from displaying a dialog requesting approval to add your channel while the user is running a different app. If you try to add a channel while running in the background, the activity's onActivityResult() method returns the status code RESULT_CANCELED.

To create a channel, follow these steps:

Create a channel builder and set its attributes. Note that the
channel type must be TYPE_PREVIEW. Add more
attributes as required.

Java

You need to save the channel ID in order to add programs to the channel
later. Extract the channel ID from the returned URI:

Kotlin

var channelId = ContentUris.parseId(channelUri)

Java

long channelId = ContentUris.parseId(channelUri);

You must add a logo for your channel. Use a Uri or Bitmap. The logo
icon should be 80dp x 80dp, and it must be opaque. It is displayed under a
circular mask:

Kotlin

// Choose one or the other
storeChannelLogo(context: Context, channelId: Long, logoUri: Uri) // also works if logoUri is a URL
storeChannelLogo(context: Context, channelId: Long, logo: Bitmap)

Java

// Choose one or the other
storeChannelLogo(Context context, long channelId, Uri logoUri); // also works if logoUri is a URL
storeChannelLogo(Context context, long channelId, Bitmap logo);

Create the default channel (optional): When your app creates its first
channel, you can make it the
default channel so it appears in the home
screen immediately without any user action. Any other channels you create
aren't visible until the user explicitly
selects them.

Kotlin

TvContractCompat.requestChannelBrowsable(context, channelId)

Java

TvContractCompat.requestChannelBrowsable(context, channelId);

Make your default channel appear before your app is opened. You can
make this behavior happen by adding a BroadcastReceiver that listens for the
android.media.tv.action.INITIALIZE_PROGRAMS action, which the home screen
sends after the app is installed:

Deleting a channel

Kotlin

Java

Note: You should never delete the default channel. If you do, the user must select it again
and ask to add it to the home screen. It cannot reappear automatically. When it's added back it appears at the bottom of the home screen, just like any other newly added channel.

Kotlin

Java

long programId = ContentUris.parseId(programUri);

Adding programs to the Watch Next channel

Inserting programs into the Watch Next channel is the same as inserting programs into your own channel.

Note: The user can manually add content to the Watch Next channel at any time. However, your
app should limit its programmatic additions to traditional TV shows and movies.
Avoid adding clips and other short form content to the Watch Next channel.

There are four types of programs; select the appropriate type:

Type

Notes

WATCH_NEXT_TYPE_CONTINUE

The user stopped while watching content.

WATCH_NEXT_TYPE_NEXT

The next available program in a series the user is watching
is available. For example, if the user is watching episode 3 of a series, the app can suggest that they watch episode 4 next.

WATCH_NEXT_TYPE_NEW

New content that clearly follows what the user is watching is now available. For example, the user is watching episode number 5 from a series and episode 6 becomes available for watching.

Use TvContractCompat.buildWatchNextProgramUri(long watchNextProgramId) to create the Uri you need to update a Watch Next program.

When the user adds a program to the Watch Next channel, the system copies the program to the row. It sends the intent TvContractCompat.ACTION_PREVIEW_PROGRAM_ADDED_TO_WATCH_NEXT to notify the app that the program has been added. The intent includes two extras: the program ID that was copied and the program ID created for the program in the Watch Next channel.

Updating a program

You can change a program's information. For example, you may want to update the rental price for a the movie, or update a progress bar showing how much of a program the user has watched.

Use a PreviewProgram.Builder to set the attributes you need to change,
then call getContentResolver().update to update the program. Specify the program ID that you saved when the program was originally added:

The system displays a dialog asking the user to approve the channel.
Handle the result of the request in the onActivityResult method of your activity (Activity.RESULT_CANCELED or Activity.RESULT_OK).

Android TV home screen events

When the user interacts with the programs/channels published by the app, the home screen sends intents to the app:

The home screen sends the Uri stored in the APP_LINK_INTENT_URI attribute of a channel to the app when the user selects the channel's logo. The app should just launch its main UI or a view related to the selected channel.

The home screen sends the Uri stored in the INTENT_URI attribute of a program to the app when the user selects a program. The app should play the selected content.

The user can indicate that they are no longer interested in a program and want it removed from the home screen's UI. The system removes the program from the UI and sends the app that owns the program an intent (android.media.tv.ACTION_PREVIEW_PROGRAM_BROWSABLE_DISABLED or android.media.tv.ACTION_WATCH_NEXT_PROGRAM_BROWSABLE_DISABLED) with the program's ID. The app should remove the program from the provider and should NOT reinsert it.

Make sure to create intent filters for all the Uris that the home screen sends for user interactions; for example:

Best practices

Many TV apps require that users login. In this case the BroadcastReceiver
that listens for android.media.tv.action.INITIALIZE_PROGRAMS should suggest
channel content for unauthenticated users. For example, your app can initially
show the best content or currently popular content. After the user logs in, it
can show personalized content. This is a great chance for apps to up-sell
users before they login. The
leanback-homescreen-channels
sample app shows how to load channels after your app is installed (or after
device setup if your app is pre-installed).

When your app is not in the foreground and you need to update a channel or a
program, use the JobScheduler to schedule the work (see:
JobScheduler
and JobService).

The system can revoke your app's provider permissions if your app misbehaves
(for example: continuously spamming the provider with data). Make sure you
wrap the code that accesses the provider with try-catch clauses to handle
security exceptions.

Before updating programs and channels, query the provider for the data you
need to update and reconcile the data. For example, there is no need to update
a program that the user wants removed from the UI. Use a background job that
inserts/updates your data into the provider after querying for the existing
data and then requesting approval for your channels. You can run this job when
the app starts and whenever the app needs to update its data.

Use unique Uris for all images (logos, icons, content images). Be sure to use a different Uri when you update an image. All images are cached. If you do not change the Uri when you change the image, the old image will continue to appear.

Remember that WHERE clauses are not allowed and calls to the providers with WHERE clauses will throw a security exception.

Attributes

This section describes the channel and program attributes separately.

Channel attributes

You must specify these attributes for every channel:

Attribute

Notes

TYPE

set to TYPE_PREVIEW.

DISPLAY_NAME

set to the name of the channel.

APP_LINK_INTENT_URI

When the user selects the channel's logo the system sends an intent to start an activity that presents content relevant to the channel. Set this attribute to the Uri used in the intent filter for that activity.

In addition, a channel also has six fields reserved for internal app usage. These fields can be used to store keys or other values that can help the app map the channel to its internal data structure: