From d5cf1e0dbc36f7b426adc6723e7e767e65a7a018 Mon Sep 17 00:00:00 2001 From: Scott Rowe Date: Tue, 24 Mar 2015 16:49:31 -0700 Subject: docs: ATV Catalog Browser Update Change-Id: I00d002eea2c3ec3b6a06c4cefa78a000fe69b81d --- docs/html/images/tv/custom-head.png | Bin 0 -> 234684 bytes docs/html/training/tv/index.jd | 4 +- docs/html/training/tv/playback/browse.jd | 318 +++++++++++++++++++++++++------ docs/html/training/tv/playback/card.jd | 10 +- 4 files changed, 272 insertions(+), 60 deletions(-) create mode 100644 docs/html/images/tv/custom-head.png diff --git a/docs/html/images/tv/custom-head.png b/docs/html/images/tv/custom-head.png new file mode 100644 index 000000000000..094e490b607b Binary files /dev/null and b/docs/html/images/tv/custom-head.png differ diff --git a/docs/html/training/tv/index.jd b/docs/html/training/tv/index.jd index d52e1e804285..9be76924c630 100644 --- a/docs/html/training/tv/index.jd +++ b/docs/html/training/tv/index.jd @@ -8,4 +8,6 @@ page.image=design/tv/images/focus.png

These classes teach you how to build apps for TV devices.

-

Note: For details on how to publish your TV apps in Google Play, see Distributing to Android TV.

\ No newline at end of file +

Note: For details on how to publish your TV +apps in Google Play, see +Distributing to Android TV.

\ No newline at end of file diff --git a/docs/html/training/tv/playback/browse.jd b/docs/html/training/tv/playback/browse.jd index 9c81597054a5..fee6a74787ad 100644 --- a/docs/html/training/tv/playback/browse.jd +++ b/docs/html/training/tv/playback/browse.jd @@ -11,25 +11,35 @@ trainingnavtop=true

This lesson teaches you to

  1. Create a Media Browse Layout
  2. +
  3. Customize the Header Views
  4. Display Media Lists
  5. Update the Background
+

Try it out

+

- Media apps that run on TV need to allow users to browse its content offerings, make a + A media app that runs on a TV needs to allow users to browse its content offerings, make a selection, and start playing content. The content browsing experience for apps of this type should be simple and intuitive, as well as visually pleasing and engaging.

This lesson discusses how to use the classes provided by the v17 leanback support library to - implement a user interface for browsing music or videos from your app's media catalog. + "{@docRoot}tools/support-library/features.html#v17-leanback">v17 leanback support library + to implement a user interface for browsing music or videos from your app's media catalog.

+App main screen +

Figure 1. The +Leanback sample app browse fragment displays video catalog data.

+

Create a Media Browse Layout

@@ -37,69 +47,270 @@ trainingnavtop=true The {@link android.support.v17.leanback.app.BrowseFragment} class in the leanback library allows you to create a primary layout for browsing categories and rows of media items with a minimum of code. The following example shows how to create a layout that contains a {@link - android.support.v17.leanback.app.BrowseFragment}: + android.support.v17.leanback.app.BrowseFragment} object:

+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/main_frame"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <fragment
+        android:name="com.example.android.tvleanback.ui.MainFragment"
+        android:id="@+id/main_browse_fragment"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent" />
+
+</FrameLayout>
+
+ +

The application's main activity sets this view, as shown in the following example:

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

The {@link android.support.v17.leanback.app.BrowseFragment} methods populate the view with the +video data and UI elements and set the layout parameters such as the icon, title, and whether +category headers are enabled.

+ + + +

The application's subclass that implements the +{@link android.support.v17.leanback.app.BrowseFragment} methods also sets +up event listeners for user actions on the UI elements, and prepares the background +manager, as shown in the following example:

+ +
+public class MainFragment extends BrowseFragment implements
+        LoaderManager.LoaderCallbacks<HashMap<String, List<Movie>>> {
+
+...
+
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+
+        loadVideoData();
+
+        prepareBackgroundManager();
+        setupUIElements();
+        setupEventListeners();
+    }
+...
+
+    private void prepareBackgroundManager() {
+        mBackgroundManager = BackgroundManager.getInstance(getActivity());
+        mBackgroundManager.attach(getActivity().getWindow());
+        mDefaultBackground = getResources()
+            .getDrawable(R.drawable.default_background);
+        mMetrics = new DisplayMetrics();
+        getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);
+    }
+
+    private void setupUIElements() {
+        setBadgeDrawable(getActivity().getResources()
+            .getDrawable(R.drawable.videos_by_google_banner));
+        // Badge, when set, takes precedent over title
+        setTitle(getString(R.string.browse_title));
+        setHeadersState(HEADERS_ENABLED);
+        setHeadersTransitionOnBackEnabled(true);
+        // set headers background color
+        setBrandColor(getResources().getColor(R.color.fastlane_background));
+        // set search icon color
+        setSearchAffordanceColor(getResources().getColor(R.color.search_opaque));
+    }
+
+    private void loadVideoData() {
+        VideoProvider.setContext(getActivity());
+        mVideosUrl = getActivity().getResources().getString(R.string.catalog_url);
+        getLoaderManager().initLoader(0, null, this);
+    }
+
+    private void setupEventListeners() {
+        setOnSearchClickedListener(new View.OnClickListener() {
+
+            @Override
+            public void onClick(View view) {
+                Intent intent = new Intent(getActivity(), SearchActivity.class);
+                startActivity(intent);
+            }
+        });
+
+        setOnItemViewClickedListener(new ItemViewClickedListener());
+        setOnItemViewSelectedListener(new ItemViewSelectedListener());
+    }
+...
+
+ +

Set UI Elements

+ +

In the sample above, the private method setupUIElements() calls several of the +{@link android.support.v17.leanback.app.BrowseFragment} methods to style the media catalog browser: +

+ + + + + +

The browse fragment shown in figure 1 lists the video category names (the row headers) in the +left pane. Text views display these category names from the video database. You can customize the +header to include additional views in a more complex layout. The following sections show how to +include an image view that displays an icon next to the category name, as shown in figure 2.

+ +App main screen +

Figure 2. The row headers in the browse fragment, with both an icon +and a text label.

+ +

The layout for the row header is defined as follows:

+ +
+<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-  android:layout_width="match_parent"
-  android:layout_height="match_parent"
-  android:orientation="vertical"
-  >
-
-  <fragment
-      android:name="android.support.v17.leanback.app.BrowseFragment"
-      android:id="@+id/browse_fragment"
-      android:layout_width="match_parent"
-      android:layout_height="match_parent"
-      />
+    android:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <ImageView
+        android:id="@+id/header_icon"
+        android:layout_width="32dp"
+        android:layout_height="32dp" />
+    <TextView
+        android:id="@+id/header_label"
+        android:layout_marginTop="6dp"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content" />
+
 </LinearLayout>
 
-

- In order to work with this layout in an activity, retrieve the {@link - android.support.v17.leanback.app.BrowseFragment} element from the layout. Use the methods in this - class to set display parameters such as the icon, title, and whether category headers are enabled. - The following code sample demonstrates how to set the layout parameters for a {@link - android.support.v17.leanback.app.BrowseFragment} in a layout: +

Use a {@link android.support.v17.leanback.widget.Presenter} and implement the +abstract methods to create, bind, and unbind the view holder. The following +example shows how to bind the viewholder with two views, an +{@link android.widget.ImageView} and a {@link android.widget.TextView}.

-public class BrowseMediaActivity extends Activity {
+public class IconHeaderItemPresenter extends Presenter {
+    @Override
+    public ViewHolder onCreateViewHolder(ViewGroup viewGroup) {
+        LayoutInflater inflater = (LayoutInflater) viewGroup.getContext()
+                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
-    public static final String TAG ="BrowseActivity";
+        View view = inflater.inflate(R.layout.icon_header_item, null);
 
-    protected BrowseFragment mBrowseFragment;
+        return new ViewHolder(view);
+    }
 
     @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.browse_fragment);
+    public void onBindViewHolder(ViewHolder viewHolder, Object o) {
+        HeaderItem headerItem = ((ListRow) o).getHeaderItem();
+        View rootView = viewHolder.view;
 
-        final FragmentManager fragmentManager = getFragmentManager();
-        mBrowseFragment = (BrowseFragment) fragmentManager.findFragmentById(
-                R.id.browse_fragment);
+        ImageView iconView = (ImageView) rootView.findViewById(R.id.header_icon);
+        Drawable icon = rootView.getResources().getDrawable(R.drawable.ic_action_video, null);
+        iconView.setImageDrawable(icon);
 
-        // Set display parameters for the BrowseFragment
-        mBrowseFragment.setHeadersState(BrowseFragment.HEADERS_ENABLED);
-        mBrowseFragment.setTitle(getString(R.string.app_name));
-        mBrowseFragment.setBadgeDrawable(getResources().getDrawable(
-                R.drawable.ic_launcher));
-        mBrowseFragment.setBrowseParams(params);
+        TextView label = (TextView) rootView.findViewById(R.id.header_label);
+        label.setText(headerItem.getName());
+    }
 
+    @Override
+    public void onUnbindViewHolder(ViewHolder viewHolder) {
+    // no op
     }
 }
 
+

This example shows how to define the presenter for a complex layout with +multiple views, and you could use this pattern to do something even more complex. +However, an easier way to combine a {@link android.widget.TextView} with a +drawable resource is to use the +{@code TextView.drawableLeft} attribute. Doing it this way, you don't need the +{@link android.widget.ImageView} shown here.

+ +

In the {@link android.support.v17.leanback.app.BrowseFragment} implementation that displays the +catalog browser, use the {@link android.support.v17.leanback.app.BrowseFragment#setHeaderPresenterSelector(android.support.v17.leanback.widget.PresenterSelector) setHeaderPresenterSelector()} +method to set the presenter for the row header, as shown in the following example.

+ +
+setHeaderPresenterSelector(new PresenterSelector() {
+    @Override
+    public Presenter getPresenter(Object o) {
+        return new IconHeaderItemPresenter();
+    }
+});
+
-

Displaying Media Lists

+

Hide or Disable Headers

+ +

Sometimes you may not want the row headers to appear: when there aren't enough categories to +require a scrollable list, for example. Call the {@link android.support.v17.leanback.app.BrowseFragment#setHeadersState(int) BrowseFragment.setHeadersState()} +method during the fragment's {@link android.app.Fragment#onActivityCreated(android.os.Bundle) onActivityCreated()} +method to hide or disable the row headers. The {@link android.support.v17.leanback.app.BrowseFragment#setHeadersState(int) setHeadersState()} +method sets the initial state of the headers in the browse fragment given one of the following +constants as a parameter:

+ + + +

If either {@link android.support.v17.leanback.app.BrowseFragment#HEADERS_ENABLED} or +{@link android.support.v17.leanback.app.BrowseFragment#HEADERS_HIDDEN} is set, you can call +{@link android.support.v17.leanback.app.BrowseFragment#setHeadersTransitionOnBackEnabled(boolean) setHeadersTransitionOnBackEnabled()} +to support moving back to the row header from a selected content item in the row. This is enabled by +default (if you don't call the method), but if you want to handle the back movement yourself, you +should pass the value false to {@link android.support.v17.leanback.app.BrowseFragment#setHeadersTransitionOnBackEnabled(boolean) setHeadersTransitionOnBackEnabled()} +and implement your own back stack handling.

+ +

Display Media Lists

- The {@link android.support.v17.leanback.app.BrowseFragment} allows you to define and display - browsable media content categories and media items from a media catalog using adapters and - presenters. Adapters enable you to connect to local or online data sources that contain your - media catalog information. Presenters hold data about media items and provide layout information - for displaying an item on screen. + The {@link android.support.v17.leanback.app.BrowseFragment} class allows you + to define and display browsable media content categories and media items from + a media catalog using adapters and presenters. Adapters enable you to connect + to local or online data sources that contain your media catalog information. + Adapters use presenters to create views and bind data to those views for + displaying an item on screen.

@@ -131,11 +342,12 @@ public class StringPresenter extends Presenter {

- Once you have constructed a presenter class for your media items, you can build and attach an - adapter to the {@link android.support.v17.leanback.app.BrowseFragment} to display those items on - screen for browsing by the user. The following example code demonstrates how to construct an - adapter to display categories and items in those categories using the {@code StringPresenter} - class shown in the previous code example: + Once you have constructed a presenter class for your media items, you can build + an adapter and attach it to the {@link android.support.v17.leanback.app.BrowseFragment} + to display those items on screen for browsing by the user. The following example + code demonstrates how to construct an adapter to display categories and items + in those categories using the {@code StringPresenter} class shown in the + previous code example:

@@ -158,7 +370,7 @@ private void buildRowsAdapter() {
         listRowAdapter.add("Media Item 1");
         listRowAdapter.add("Media Item 2");
         listRowAdapter.add("Media Item 3");
-        HeaderItem header = new HeaderItem(i, "Category " + i, null);
+        HeaderItem header = new HeaderItem(i, "Category " + i);
         mRowsAdapter.add(new ListRow(header, listRowAdapter));
     }
 
@@ -170,15 +382,15 @@ private void buildRowsAdapter() {
   This example shows a static implementation of the adapters. A typical media browsing application
   uses data from an online database or web service. For an example of a browsing application that
   uses data retrieved from the web, see the
-  Android TV sample app.
+  Android Leanback sample app.
 

Update the Background

In order to add visual interest to a media-browsing app on TV, you can update the background - image as users browse through content. This technique can make interaction with your app feel - more cinematic and enjoyable for users. + image as users browse through content. This technique can make interaction with your app more + cinematic and enjoyable.

@@ -211,8 +423,8 @@ protected OnItemViewSelectedListener getDefaultItemViewSelectedListener() { @Override public void onItemSelected(Object item, Row row) { if (item instanceof Movie ) { - URI uri = ((Movie)item).getBackdropURI(); - updateBackground(uri); + Drawable background = ((Movie)item).getBackdropDrawable(); + updateBackground(background); } else { clearBackground(); } diff --git a/docs/html/training/tv/playback/card.jd b/docs/html/training/tv/playback/card.jd index 8ac75fd35f2a..a3a987252a8a 100644 --- a/docs/html/training/tv/playback/card.jd +++ b/docs/html/training/tv/playback/card.jd @@ -32,9 +32,10 @@ class used in this lesson displays an image for the content along with the media Android Leanback sample app, available on GitHub. Use this sample code to start your own app.

-App main screen +App card view

Figure 1. The -Leanback sample app browse fragment with a card presenter displaying card view objects.

+Leanback sample app image card view when selected.

+

Create a Card Presenter

@@ -147,10 +148,7 @@ and {@link android.view.View#setFocusableInTouchMode(boolean) setFocusableInTouc

When the user selects the {@link android.support.v17.leanback.widget.ImageCardView}, it expands -to reveal its text area with the background color you specify, as shown in figure 2.

+to reveal its text area with the background color you specify, as shown in figure 1.

-App card view -

Figure 2. The -Leanback sample app image card view when selected.

-- cgit v1.2.3-59-g8ed1b