Hi, I'm Ankur Kotwal, a developer advocate at Google.
Today, I'm going to talk about how
you can start building apps for Android TV.
Great experiences on Android TV are casual, cinematic,
and simple.
A TV screen is typically viewed from about 10 feet away,
and it's much larger than other Android devices.
Building an effective user interface for TV
requires a different approach and different tools.
To make your life easier, we provide the Leanback Support Library.
It includes APIs, resources, and widgets to build apps for TV.
You can build apps for Android TV without it,
but why would you?
It makes building high-quality user experiences
for the big screen super easy.
When you create a project and choose TV as your form factor,
the Leanback Support Library is automatically
added into your project.
If you're not using Android Studio,
you can declare these dependencies
through your build.gradle file.
The Android Support Library is available
in the Android Repository Package.
So if you haven't installed this package yet,
you can do so via the SDK manager.
Now let's look at our sample app.
First, you must declare an activity for TV
in the Manifest using the Leanback Launcher Intent Filter.
This intent filter identifies your app
as being built for Android TV.
At the same time, setting this intent filter
also identifies which activity in your app
should be launched when a user selects the app icon on TV.
The Leanback Library provides a standard theme
for TV activities called theme.leanback.
This provides a consistent visual style for TV apps.
And it is recommended for any TV app that
uses the Leanback Library classes.
The Leanback Support Library is designed
based on the model-presenter-view pattern.
A derivative of the model-view-controller pattern.
There are strict requirements for model objects.
An object adapter can be used to define any kinds of models,
and therefore you should be able to update your existing app
without changing your model.
It's also possible to connect local or online data sources
that contain your media using object adapters.
And you can define how these items
will look using presenters.
The Leanback Library also provides pre-built fragments
for browsing and interacting with media catalogs,
and you can take advantage of this
when you're building your own media browsing apps.
Here is the initial layout for the app.
It consists of a single subclass of the BrowseFragment
from the Leanback Support Library.
The BrowseFragment allows you to create a primary layout
for browsing categories and rows of media items
with minimum effort.
As you can see in this sample app.
Just adding this class to our layout
has created a multi-pane layout
along with standard navigational design
and polished transition animations.
Let's look at how we can populate
the fragment with some content.
A single ArrayObjectAdapter is used
to define a list of ListRow objects.
And a ListRow presenter is used to display them.
Categories are automatically displayed vertically
on the left, whilst media for each category
is a horizontal scrolling list.
As the user drills deeper into the category,
the categories themselves will collapse
and will be displayed per row as a header item.
Each ListRow object is composed of one header item
and another ArrayObjectAdapter.
The header item describes the metadata
of this row-- in this case, the category name.
And the ArrayObjectAdapter contains the media items
for each category.
Here, a user-defined CardPresenter
is used to display each media item.
You need to present this yourself.
All of these classes--ArrayObjectAdapter,
ListRepresenter, and ListRow,
are provided by the Leanback Support Library.
The only thing you need to implement yourself
is a presenter for displaying your media items.
When you create a ListRow object,
you can pass your own custom presenter
to define how each individual item will look.
In this example, CardPresenter displays each media item
as a card filled with a thumbnail image.
The CardPresenter extends the Presenter class,
overriding onCreateViewHolder to generate views
and unbindViewHolder to bind objects
to views on demand.
One thing you should keep in mind
is that every interactive view in Android TV
should be navigable by the directional pad.
To achieve this, you need to set the Focusable
and FocusableInTouchMode properties to true, like here.
In order to interact with user input,
you need to add the OnItemClick listener to the BrowseFragment.
During the callback, you can fire an intent
to start the item DetailsActivity
or replace the BrowseFragment with the DetailsFragment
via the fragment manager.
The DetailsFragment, also found in the Support Library,
allows you to create a primary layout
for displaying detailed information.
And again, it does so with minimum effort on your part.
Again, a single ArrayObjectAdapter
is used to define a list of rows.
The DetailsOverviewRow is the row
which contains the specifics of a media item.
In order to render the DetailsOverview object itself,
you use the DetailsOverviewRow presenter.
This row consists of an image,
a DescriptionView,
and a series of actions.
You can easily preview an image,
or add actions to this object
by calling the methods setImageBitmap or addAction.
For displaying the DescriptionView,
you can use the AbstractDetailsDescription
presenter class.
It is also possible to add more content,
such as related videos.
And to do so, you can add an additional row
into the ArrayObjectAdapter with a presenter to display them.
Here, an additional ListRow object
is at the end of the ArrayObjectAdapter
with a list of related videos.
And again, the ListRow presenter is used to display this row.
So these are the basic steps
to get started with Android TV development.
In summary, the Leanback Support Library
makes it super easy to build high-quality experiences.
The BrowseFragment and DetailsFragment
are the easy way to jump start your Android TV development.
The code shown in this dev byte
is part of the samples listed here.
And you can also discover more about the many unique features
of the Android TV platform, such as recommendations
and the background manager,
at developer.android.com/tv.
I look forward to seeing your apps on the big screen.