Home | History | Annotate | Download | only in start
      1 page.title=Get Started with TV Apps
      2 page.tags="leanback","recyclerview","launcher"
      3 
      4 trainingnavtop=true
      5 startpage=true
      6 
      7 @jd:body
      8 
      9 <div id="tb-wrapper">
     10 <div id="tb">
     11   <h2>This lesson teaches you how to</h2>
     12   <ol>
     13     <li><a href="#media">Determine Media Format Support</a></li>
     14     <li><a href="#dev-project">Setup a TV Project</a></li>
     15     <li><a href="#tv-libraries">Add TV Support Libraries</a></li>
     16     <li><a href="#build-it">Build TV Apps</a></li>
     17     <li><a href="#run">Run TV Apps</a></li>
     18   </ol>
     19   <h2>You should also read</h2>
     20   <ol>
     21     <li><a href="{@docRoot}design/tv/index.html">
     22       TV Design</a></li>
     23     <li><a href="{@docRoot}training/tv/start/layouts.html">
     24       Building TV Layouts</a></li>
     25   </ol>
     26 </div>
     27 </div>
     28 
     29 <p>
     30   TV apps use the same structure as those for phones and tablets. This similarity means you can
     31   modify your existing apps to also run on TV devices or create new apps based on what you already
     32   know about building apps for Android.
     33 </p>
     34 
     35 <p class="note">
     36   <strong>Important:</strong> There are specific requirements your app must meet to
     37   qualify as an Android TV app on Google Play. For more information, see the requirements listed
     38   in <a href="{@docRoot}distribute/essentials/quality/tv.html">TV App Quality</a>.
     39 </p>
     40 
     41 <p>
     42   This lesson describes how to prepare your development environment for building TV apps, and the
     43   minimum required changes to enable an app to run on TV devices.
     44 </p>
     45 
     46 <h2 id="media">Determine Media Format Support</h2>
     47 
     48 <p>See the following documentation for information about the codecs, protocols, and formats
     49 supported by Android TV.</p>
     50 
     51 <ul>
     52   <li><a href="{@docRoot}guide/appendix/media-formats.html">Supported Media Formats</a></li>
     53   <li><a class="external-link" href="https://source.android.com/devices/drm.html">DRM</a></li>
     54   <li><code><a href="{@docRoot}reference/android/drm/package-summary.html">android.drm</a></code></li>
     55   <li><a href="{@docRoot}guide/topics/media/exoplayer.html">ExoPlayer</a></li>
     56   <li>{@link android.media.MediaPlayer android.media.MediaPlayer}</li>
     57 </ul>
     58 
     59 <h2 id="dev-project">Set up a TV Project</h2>
     60 
     61 <p>
     62   This section discusses how to modify an existing app to run on TV devices, or create a new one.
     63   These are the main components you must use to create an app that runs on TV devices:
     64 </p>
     65 
     66 <ul>
     67   <li><strong>Activity for TV</strong> (Required) - In your application manifest,
     68     declare an activity that is intended to run on TV devices.</li>
     69   <li><strong>TV Support Libraries</strong> (Optional) - There are several
     70     <a href="#tv-libraries">Support Libraries</a>
     71     available for TV devices that provide widgets for building user interfaces.</li>
     72 </ul>
     73 
     74 
     75 <h3 id="prerequisites">Prerequisites</h3>
     76 
     77 <p>Before you begin building apps for TV, you must:</p>
     78 
     79 <ul>
     80   <li><strong><a href="{@docRoot}studio/intro/update.html#GetTools">
     81     Update your SDK tools to version 24.0.0 or higher</a></strong>
     82     <br>
     83     The updated SDK tools enable you to build and test apps for TV.
     84   </li>
     85   <li><strong><a href="{@docRoot}studio/intro/update.html#GetTools">
     86     Update your SDK with Android 5.0 (API 21) or higher</a></strong>
     87     <br>
     88     The updated platform version provides new APIs for TV apps.
     89   </li>
     90   <li><strong><a href="{@docRoot}studio/projects/create-project.html">
     91     Create or update your app project</a></strong>
     92     <br>
     93     In order to access new APIs for TV devices, you must create a project or modify an existing
     94     project that targets Android 5.0 (API level 21) or higher.
     95   </li>
     96 </ul>
     97 
     98 
     99 <h3 id="tv-activity">Declare a TV Activity</h3>
    100 
    101 <p>An application intended to run on TV devices must declare a launcher activity for TV
    102   in its manifest using a {@link android.content.Intent#CATEGORY_LEANBACK_LAUNCHER} intent filter.
    103   This filter identifies your app as being enabled for TV, and is required for your app to be
    104   considered a TV app in Google Play. Declaring this intent also identifies which activity
    105   in your app to launch when a user selects its icon on the TV home screen.</p>
    106 
    107 <p>The following code snippet shows how to include this intent filter in your manifest:</p>
    108 
    109 <pre>
    110 &lt;application
    111   android:banner="&#64;drawable/banner" &gt;
    112   ...
    113   &lt;activity
    114     android:name=&quot;com.example.android.MainActivity&quot;
    115     android:label=&quot;@string/app_name&quot; &gt;
    116 
    117     &lt;intent-filter&gt;
    118       &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
    119       &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
    120     &lt;/intent-filter&gt;
    121   &lt;/activity&gt;
    122 
    123   &lt;activity
    124     android:name=&quot;com.example.android.<strong>TvActivity</strong>&quot;
    125     android:label=&quot;&#64;string/app_name&quot;
    126     android:theme=&quot;&#64;style/Theme.Leanback&quot;&gt;
    127 
    128     &lt;intent-filter&gt;
    129       &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
    130       &lt;category android:name="<strong>android.intent.category.LEANBACK_LAUNCHER</strong>" /&gt;
    131     &lt;/intent-filter&gt;
    132 
    133   &lt;/activity&gt;
    134 &lt;/application&gt;
    135 </pre>
    136 
    137 <p>
    138   The second activity manifest entry in this example specifies that activity as the one to
    139   launch on a TV device.
    140 </p>
    141 
    142 <p class="caution">
    143   <strong>Caution:</strong> If you do not include the
    144   {@link android.content.Intent#CATEGORY_LEANBACK_LAUNCHER} intent filter in
    145   your app, it is not visible to users running the Google Play store on TV devices. Also, if your
    146   app does not have this filter when you load it onto a TV device using developer tools, the app
    147   does not appear in the TV user interface.
    148 </p>
    149 
    150 <p>
    151   If you are modifying an existing app for use on TV, your app should not use the same
    152   activity layout for TV that it does for phones and tablets. The user interface of your TV app (or
    153   TV portion of your existing app) should provide a simpler interface that can be easily navigated
    154   using a remote control from a couch. For guidelines on designing an app for TV, see the <a href=
    155   "{@docRoot}design/tv/index.html">TV Design</a> guide. For more information on the minimum
    156   implementation requirements for interface layouts on TV, see <a href=
    157   "{@docRoot}training/tv/start/layouts.html">Building TV Layouts</a>.
    158 </p>
    159 
    160 <h3 id="leanback-req">Declare Leanback support</h3>
    161 
    162 <p>
    163   Declare that your app uses the Leanback user interface required by Android TV. If you are developing
    164   an app that runs on mobile (phones, wearables, tablets, etc.) as well as Android TV, set the
    165   {@code required} attribute value to {@code false}. If you set the {@code required} attribute value
    166   to {@code true}, your app will run only on devices that use the Leanback UI.
    167 </p>
    168 
    169 <pre>
    170 &lt;manifest&gt;
    171     &lt;uses-feature android:name="android.software.leanback"
    172         android:required="false" /&gt;
    173     ...
    174 &lt;/manifest&gt;
    175 </pre>
    176 
    177 <h3 id="no-touchscreen">Declare touchscreen not required</h3>
    178 
    179 <p>
    180   Applications that are intended to run on TV devices do not rely on touch screens for input. In
    181   order to make this clear, the manifest of your TV app must declare that a the {@code
    182   android.hardware.touchscreen} feature is not required. This setting identifies your app as being
    183   able to work on a TV device, and is required for your app to be considered a TV app in Google
    184   Play. The following code example shows how to include this manifest declaration:
    185 </p>
    186 
    187 <pre>
    188 &lt;manifest&gt;
    189     &lt;uses-feature android:name="android.hardware.touchscreen"
    190               android:required="false" /&gt;
    191     ...
    192 &lt;/manifest&gt;
    193 </pre>
    194 
    195 <p class="caution">
    196   <strong>Caution:</strong> You must declare that a touch screen is not required in your app
    197   manifest, as shown this example code, or your app cannot appear in the Google Play store on TV
    198   devices.
    199 </p>
    200 
    201 <h3 id="banner">Provide a home screen banner</h3>
    202 
    203 <p>
    204   An application must provide a home screen banner image for each localization
    205   if it includes a Leanback launcher intent filter. The banner is the app launch
    206   point that appears on the home screen in the apps and games rows. When designing your banner,
    207   follow the design requirements described in
    208   <a href="{@docRoot}design/tv/patterns.html#banner">Banners</a>.
    209   To add the banner to your app, describe the banner in the manifest as follows:
    210 </p>
    211 
    212 <pre>
    213 &lt;application
    214     ...
    215     android:banner="&#64;drawable/banner" &gt;
    216 
    217     ...
    218 &lt;/application&gt;
    219 </pre>
    220 
    221 <p>
    222   Use the <a href="{@docRoot}guide/topics/manifest/application-element.html#banner">{@code android:banner}</a>
    223   attribute with the <a href="{@docRoot}guide/topics/manifest/application.html"><code>&lt;application&gt;</code></a>
    224   tag to supply a default banner for all application activities, or with the
    225   <a href="{@docRoot}guide/topics/manifest/activity-element.html"><code>&lt;activity&gt;</code></a>
    226   tag to supply a banner for a specific activity.
    227 </p>
    228 
    229 <h3 id="transition-color">Change the launcher color</h3>
    230 
    231 <p>When a TV app launches, the system displays an animation that resembles an expanding, filled
    232   circle. To customize the color of this animation, set the <code>android:colorPrimary</code>
    233   attribute of your TV app or activity to a specific color. You should also set two additional
    234   transition overlap attributes to <code>true</code>, as shown in the following snippet from a
    235   theme resource XML file:</p>
    236 
    237 <pre>
    238 &lt;resources&gt;
    239     &lt;style ... &gt;
    240       &lt;item name="android:colorPrimary"&gt;&#64;color/primary&lt;/item&gt;
    241       &lt;item name="android:windowAllowReturnTransitionOverlap"&gt;true&lt;/item&gt;
    242       &lt;item name="android:windowAllowEnterTransitionOverlap"&gt;true&lt;/item&gt;
    243     &lt;/style&gt;
    244 &lt;/resources&gt;
    245 </pre>
    246 
    247 <p>For more information about working with themes and styles, see
    248 <a href="{@docRoot}guide/topics/ui/themes.html">Styles and Themes</a>.</p>
    249 
    250 <h2 id="tv-libraries">Add TV Support Libraries</h3>
    251 
    252 <p>
    253   The Android SDK includes support libraries that are intended for use with TV apps. These
    254   libraries provide APIs and user interface widgets for use on TV devices. The libraries are
    255   located in the {@code <sdk>/extras/android/support/} directory. Here is a list of the
    256   libraries and their general purpose:
    257 </p>
    258 
    259 <ul>
    260   <li><a href="{@docRoot}tools/support-library/features.html#v17-leanback">
    261     <strong>v17 leanback library</strong></a> - Provides user interface widgets for TV apps,
    262     particularly for apps that do media playback.
    263   </li>
    264   <li><a href="{@docRoot}tools/support-library/features.html#v7-recyclerview">
    265     <strong>v7 recyclerview library</strong></a> - Provides classes for managing display of long
    266     lists in a memory efficient manner. Several classes in the v17 leanback library depend on the
    267     classes in this library.
    268   </li>
    269   <li><a href="{@docRoot}tools/support-library/features.html#v7-cardview">
    270     <strong>v7 cardview library</strong></a> - Provides user interface widgets for displaying
    271     information cards, such as media item pictures and descriptions.
    272   </li>
    273 </ul>
    274 
    275 <p class="note">
    276   <strong>Note:</strong> You are not required to use these support libraries for your TV app.
    277   However, we strongly recommend using them, particularly for apps that provide a media catalog
    278   browsing interface.
    279 </p>
    280 
    281 <p>
    282   If you decide to use the v17 leanback library for your app, you should note that it is dependent
    283   on the <a href="{@docRoot}tools/support-library/features.html#v4">v4 support library</a>. This
    284   means that apps that use the leanback support library should include all of these support
    285   libraries:
    286 </p>
    287 
    288 <ul>
    289   <li>v4 support library</li>
    290   <li>v7 recyclerview support library</li>
    291   <li>v17 leanback support library</li>
    292 </ul>
    293 
    294 <p>
    295   The v17 leanback library contains resources, which require you to take specific steps to include
    296   it in app projects. For instructions on importing a support library with resources, see
    297   <a href="{@docRoot}tools/support-library/setup.html#libs-with-res">Support Library Setup</a>.
    298 </p>
    299 
    300 
    301 <h2 id="build-it">Build TV Apps</h2>
    302 
    303 <p>After you have completed the steps described above, it's time to start building apps for
    304   the big screen! Check out these additional topics to help you build your app for TV:
    305 
    306 <ul>
    307   <li>
    308     <a href="{@docRoot}training/tv/playback/index.html">Building TV Playback Apps</a> - TVs are
    309     built to entertain, so Android provides a set of user interface tools and widgets for building
    310     TV apps that play videos and music, and let users browse for the content they want.
    311   </li>
    312   <li>
    313     <a href="{@docRoot}training/tv/discovery/index.html">Helping Users Find Your Content on TV</a> -
    314     With all the content choices at users' fingertips, helping them find content they enjoy is almost
    315     as important as providing that content. This training discusses how to surface your content on
    316     TV devices.
    317   </li>
    318   <li>
    319     <a href="{@docRoot}training/tv/games/index.html">Building TV Games</a> - TV devices are a great
    320     platform for games. See this topic for information on building great game experiences for TV.
    321   </li>
    322     <li>
    323     <a href="{@docRoot}training/tv/tif/index.html">Building Live TV Apps</a> - Present your video
    324     content in a linear, "broadcast TV" style with channels and programs that your users can access
    325     through a program guide as well as the channel up/down buttons.
    326   </li>
    327 
    328 </ul>
    329 
    330 
    331 <h2 id="run">Run TV Apps</h2>
    332 
    333 <p>
    334   Running your app is an important part of the development process. The AVD Manager in the Android
    335   SDK provides the device definitions that allow you to create virtual TV devices for running and
    336   testing your applications.
    337 </p>
    338 
    339 <p>To create an virtual TV device:</p>
    340 
    341 <ol>
    342   <li>Start the AVD Manager. For more information, see the
    343     <a href="{@docRoot}tools/help/avd-manager.html">AVD Manager</a> help.</li>
    344   <li>In the AVD Manager dialog, click the <strong>Device Definitions</strong> tab.</li>
    345   <li>Select one of the Android TV device definitions and click <strong>Create AVD</strong>.</li>
    346   <li>Select the emulator options and click <strong>OK</strong> to create the AVD.
    347     <p class="note">
    348       <strong>Note:</strong> For best performance of the TV emulator device, enable the <strong>Use
    349       Host GPU</strong> option and, where supported, use virtual device acceleration. For
    350       more information on hardware acceleration of the emulator, see
    351       <a href="{@docRoot}tools/devices/emulator.html#acceleration">Using the Emulator</a>.
    352     </p>
    353   </li>
    354 </ol>
    355 
    356 <p>To test your application on the virtual TV device:</p>
    357 
    358 <ol>
    359   <li>Compile your TV application in your development environment.</li>
    360   <li>Run the application from your development environment and choose the TV virtual device as
    361   the target.</li>
    362 </ol>
    363 
    364 <p>
    365   For more information about using emulators see, <a href="{@docRoot}tools/devices/emulator.html">
    366   Using the Emulator</a>. For more information on deploying apps from Android Studio to virtual
    367   devices, see <a href="{@docRoot}studio/debug/index.html">Debugging with Android
    368   Studio</a>.
    369 </p>
    370