1page.title=Building Layouts for TV
2page.tags=tv
3helpoutsWidget=true
4
5trainingnavtop=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="#themes">Use Layout Themes for TV</a></li>
14    <li><a href="#structure">Build Basic TV Layouts</a></li>
15    <li><a href="#visibility">Build Useable Text and Controls</a></li>
16    <li><a href="#density-resources">Manage Layout Resources for TV</a></li>
17    <li><a href="#anti-patterns">Avoid Layout Anti-Patterns</a></li>
18    <li><a href="#large-bitmaps">Handle Large Bitmaps</a></li>
19    <li><a href="#advertising">Provide Effective Advertising</a></li>
20  </ol>
21  <h2>You should also read</h2>
22  <ol>
23    <li><a href="{@docRoot}design/tv/index.html">Android TV Design</a></li>
24  </ol>
25</div>
26</div>
27
28<p>
29  A TV screen is typically viewed from about 10 feet away, and while it is much larger than most
30  other Android device displays, this type of screen does not provide the same level of precise
31  detail and color as a smaller device. These factors require you to create app layouts with TV
32  devices in mind in order to create a useful and enjoyable user experience.
33</p>
34
35<p>
36  This lesson describes the minimum requirements and implementation details for building effective
37  layouts in TV apps.
38</p>
39
40<h2 id="themes">Use Layout Themes for TV</h2>
41
42<p>
43  Android <a href="{@docRoot}guide/topics/ui/themes.html">Themes</a> can provide a basis for
44  layouts in your TV apps. You should use a theme to modify the display of your app activities that
45  are meant to run on a TV device. This section explains which themes you should use.
46</p>
47
48<h3 id="leanback-theme">Leanback theme</h3>
49
50<p>
51  A support library for TV user interfaces called the <a href=
52  "{@docRoot}tools/support-library/features.html#v17-leanback">v17 leanback library</a> provides a
53  standard theme for TV activities, called {@code Theme.Leanback}. This theme establishes a
54  consistent visual style for TV apps. Use of this theme is recommended for most TV apps. This
55  theme is strongly recommended for any TV app that uses v17 leanback classes. The following code
56  sample shows how to apply this theme to a given activity within an app:
57</p>
58
59<pre>
60&lt;activity
61  android:name="com.example.android.TvActivity"
62  android:label="&#64;string/app_name"
63  <strong>android:theme="&#64;style/Theme.Leanback"</strong>&gt;
64</pre>
65
66
67<h3 id="notitle-theme">NoTitleBar theme</h3>
68
69<p>
70  The title bar is a standard user interface element for Android apps on phones and tablets, but it
71  is not appropriate for TV apps. If you are not using v17 leanback classes, you should apply this
72  theme to your TV activities to suppress the display of a title bar. The following code example
73  from a TV app manifest demonstrates how to apply this theme to remove the display of a title bar:
74</p>
75
76<pre>
77&lt;application&gt;
78  ...
79
80  &lt;activity
81    android:name="com.example.android.TvActivity"
82    android:label="&#64;string/app_name"
83    <strong>android:theme="&#64;android:style/Theme.NoTitleBar"</strong>&gt;
84    ...
85
86  &lt;/activity&gt;
87&lt;/application&gt;
88</pre>
89
90
91<h2 id="structure">Build Basic TV Layouts</h2>
92
93<p>Layouts for TV devices should follow some basic guidelines to ensure they are usable and
94  effective on large screens. Follow these tips to build landscape layouts optimized for TV screens:
95</p>
96
97<ul>
98  <li>Build layouts with a landscape orientation. TV screens always display in landscape mode.</li>
99  <li>Put on-screen navigation controls on the left or right side of the screen and save the
100    vertical space for content.</li>
101  <li>Create UIs that are divided into sections, using <a href="{@docRoot}guide/components/fragments.html"
102    >Fragments</a>, and use view groups like {@link android.widget.GridView} instead of {@link
103    android.widget.ListView} to make better use of the horizontal screen space.
104  </li>
105  <li>Use view groups such as {@link android.widget.RelativeLayout} or {@link
106    android.widget.LinearLayout} to arrange views. This approach allows the system to adjust the
107    position of the views to the size, alignment, aspect ratio, and pixel density of a TV screen.</li>
108  <li>Add sufficient margins between layout controls to avoid a cluttered UI.</li>
109</ul>
110
111
112<h3 id="overscan">Overscan</h3>
113
114<p>Layouts for TV have some unique requirements due to the evolution of TV standards and the
115  desire to always present a full screen picture to viewers. For this reason, TV devices may
116  clip the outside edge of an app layout in order to ensure that the entire display is filled.
117  This behavior is generally referred to as <em>overscan</em>.
118</p>
119
120<p>
121  Avoid screen elements being clipped due to overscan and by incorporating a 10% margin
122  on all sides of your layout. This translates into a 48dp margin on the left and right edges and
123  a 27dp margin on the top and bottom of your base layouts for activities. The following
124  example layout demonstrates how to set these margins in the root layout for a TV app:
125</p>
126
127<pre>
128&lt;?xml version="1.0" encoding="utf-8"?&gt;
129&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
130  android:id="@+id/base_layout"
131  android:layout_width="match_parent"
132  android:layout_height="match_parent"
133  android:orientation="vertical"
134  android:layout_marginTop="27dp"
135  android:layout_marginLeft="48dp"
136  android:layout_marginRight="48dp"
137  android:layout_marginBottom="27dp" &gt;
138&lt;/LinearLayout&gt;
139</pre>
140
141<p class="caution">
142  <strong>Caution:</strong> Do not apply overscan margins to your layout if you are using the
143  v17 leanback classes, such as {@link android.support.v17.leanback.app.BrowseFragment} or related
144  widgets, as those layouts already incorporate overscan-safe margins.
145</p>
146
147<h2 id="visibility">Build Useable Text and Controls</h2>
148
149<p>
150  The text and controls in a TV app layout should be easily visible and navigable from a distance.
151  Follow these tips to make your user interface elements easier to see from a distance:
152</p>
153
154<ul>
155  <li>Break text into small chunks that users can quickly scan.</li>
156  <li>Use light text on a dark background. This style is easier to read on a TV.</li>
157  <li>Avoid lightweight fonts or fonts that have both very narrow and very broad strokes.
158  Use simple sans-serif fonts and anti-aliasing to increase readability.</li>
159  <li>Use Android's standard font sizes:
160<pre>
161&lt;TextView
162      android:id="@+id/atext"
163      android:layout_width="wrap_content"
164      android:layout_height="wrap_content"
165      android:gravity="center_vertical"
166      android:singleLine="true"
167      <strong>android:textAppearance="?android:attr/textAppearanceMedium"/&gt;</strong>
168</pre>
169  </li>
170  <li>Ensure that all your view widgets are large enough to be clearly visible to someone
171    sitting 10 feet away from the screen (this distance is greater for very large screens). The
172    best way to do this is to use layout-relative sizing rather than absolute sizing, and
173    density-independent pixel (dip) units instead of absolute pixel units. For example, to set the
174    width of a widget, use {@code wrap_content} instead of a pixel measurement, and to set the
175    margin for a widget, use dip values instead of px values.</li>
176</ul>
177
178<p>
179  For more information about density-independent pixels and building layouts to handle larger
180  screen sizes, see <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
181  Screens</a>.
182</p>
183
184<h2 id="density-resources">Manage Layout Resources for TV</h2>
185
186<p>The common high-definition TV display resolutions are 720p, 1080i, and 1080p.
187  Your TV layout should target a screen size of 1920 x 1080 pixels, and then allow the Android
188  system to downscale your layout elements to 720p if necessary. In general, downscaling
189  (removing pixels) does not degrade your layout presentation quality. However, upscaling can
190  cause display artifacts that degrade the quality of your layout and have a negative impact on
191  the user experience of your app.</p>
192
193<p>
194  To get the best scaling results for images, provide them as
195  <a href="{@docRoot}tools/help/draw9patch.html">9-patch image</a> elements if possible. If you
196  provide low quality or small images in your layouts, they will appear pixelated, fuzzy, or
197  grainy, which is not a good experience for the user. Use high-quality images instead.
198</p>
199
200<p>
201  For more information on optimizing layouts and resources for large screens see
202  <a href="{@docRoot}training/multiscreen/index.html">Designing for multiple screens</a>.
203</p>
204
205
206<h2 id="anti-patterns">Avoid Layout Anti-Patterns</h2>
207
208<p>
209  There are a few approaches to building layouts that you should avoid because they do not work
210  well on TV devices and lead to bad user experiences. Here are some user interface approaches you
211  should specifically <em>not</em> use when developing a layout for TV.
212</p>
213
214<ul>
215  <li><strong>Re-using phone or tablet layouts</strong> - Do not reuse layouts from a phone or
216    tablet app without modification. Layouts built for other Android device form factors are not
217    well suited for TV devices and should be simplified for operation on a TV.</li>
218  <li><strong>ActionBar</strong> - While this user interface convention is recommended for use
219    on phones and tablets, it is not appropriate for a TV interface. In particular, using an
220    action bar options menu (or any pull-down menu for that matter) is strongly discouraged, due
221    to the difficulty in navigating such a menu with a remote control.</li>
222  <li><strong>ViewPager</strong> - Sliding between screens can work great on a phone or tablet,
223    but don't try this on a TV!</li>
224</ul>
225
226<p>For more information on designing layouts that are appropriate to TV, see the
227  <a href="{@docRoot}design/tv/index.html">TV Design</a> guide.</p>
228
229
230<h2 id="large-bitmaps">Handle Large Bitmaps</h2>
231
232<p>TV devices, like any other Android device, have a limited amount of memory. If you build your
233  app layout with very high-resolution images or use many high-resolution images in the operation
234  of your app, it can quickly run into memory limits and cause out of memory errors.
235  To avoid these types of problems, follow these tips:</p>
236
237<ul>
238  <li>Load images only when they are displayed on the screen. For example, when displaying multiple
239  images in a {@link android.widget.GridView} or {@link android.widget.Gallery}, only load an image
240  when {@link android.widget.Adapter#getView getView()} is called on the
241  view's {@link android.widget.Adapter}.
242  </li>
243  <li>Call {@link android.graphics.Bitmap#recycle()} on {@link android.graphics.Bitmap} views that
244  are no longer needed.
245  </li>
246  <li>Use {@link java.lang.ref.WeakReference} for storing references to {@link
247  android.graphics.Bitmap} objects in an in-memory {@link java.util.Collection}.
248  </li>
249  <li>If you fetch images from the network, use {@link android.os.AsyncTask} to fetch and store
250  them on the device for faster access. Never do network transactions on the application's main
251  user interface thread.
252  </li>
253  <li>Scale down large images to a more appropriate size as you download them; otherwise,
254  downloading the image itself may cause an out of memory exception.
255  </li>
256</ul>
257
258<p>
259  For more information on getting the best performance when working with images, see
260  <a href="{@docRoot}training/displaying-bitmaps/index.html">Displaying Bitmaps Efficiently</a>.
261</p>
262
263<h2 id="advertising">Provide Effective Advertising</h2>
264
265<p>For the living room environment, we recommend you use video ads solutions
266that are full-screen and dismissable within 30 seconds. Functionality for
267advertising on Android TV, such as dismiss buttons and clickthroughs, must be
268accessible using the D-pad rather than touch.</p>
269
270<p>Android TV does not provide a web browser. Your ads must not attempt to
271launch a web browser or redirect to Google Play Store content that is not
272approved for Android TV devices.</p>
273
274<p class="note">
275  <strong>Note:</strong> You can use the {@link android.webkit.WebView} class
276  for logins to services like Google+ and Facebook.
277</p>