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>Screen elements that must be visible to the user at all times should be positioned within the
121overscan safe area. Adding a 5% margin of 48dp on the left and right edges and 27dp on the top and
122bottom edges to a layout ensures that screen elements in that layout will be within the overscan
123safe area.
124</p>
125
126<p>Background screen elements that the user doesn't directly interact with should not be adjusted or
127clipped to the overscan safe area. This approach ensures that background screen elements look
128correct on all screens.
129</p>
130
131<p>The following example shows a root layout that can contain background elements, and a nested
132child layout that has a 5% margin and can contain elements within the overscan safe area:
133</p>
134
135<pre>
136&lt;?xml version="1.0" encoding="utf-8"?&gt;
137&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
138   android:layout_width="match_parent"
139   android:layout_height="match_parent"
140   &gt;
141
142   &lt;!-- Screen elements that can render outside the overscan safe area go here --&gt;
143
144   &lt;!-- Nested RelativeLayout with overscan-safe margin --&gt;
145   &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
146       android:layout_width="match_parent"
147       android:layout_height="match_parent"
148       android:layout_marginTop="27dp"
149       android:layout_marginBottom="27dp"
150       android:layout_marginLeft="48dp"
151       android:layout_marginRight="48dp"&gt;
152
153      &lt;!-- Screen elements that need to be within the overscan safe area go here --&gt;
154
155   &lt;/RelativeLayout&gt;
156&lt;/RelativeLayout&gt;
157</pre>
158
159<p class="caution">
160  <strong>Caution:</strong> Do not apply overscan margins to your layout if you are using the
161  v17 leanback classes, such as {@link android.support.v17.leanback.app.BrowseFragment} or related
162  widgets, as those layouts already incorporate overscan-safe margins.
163</p>
164
165<h2 id="visibility">Build Useable Text and Controls</h2>
166
167<p>
168  The text and controls in a TV app layout should be easily visible and navigable from a distance.
169  Follow these tips to make your user interface elements easier to see from a distance:
170</p>
171
172<ul>
173  <li>Break text into small chunks that users can quickly scan.</li>
174  <li>Use light text on a dark background. This style is easier to read on a TV.</li>
175  <li>Avoid lightweight fonts or fonts that have both very narrow and very broad strokes.
176  Use simple sans-serif fonts and anti-aliasing to increase readability.</li>
177  <li>Use Android's standard font sizes:
178<pre>
179&lt;TextView
180      android:id="@+id/atext"
181      android:layout_width="wrap_content"
182      android:layout_height="wrap_content"
183      android:gravity="center_vertical"
184      android:singleLine="true"
185      <strong>android:textAppearance="?android:attr/textAppearanceMedium"/&gt;</strong>
186</pre>
187  </li>
188  <li>Ensure that all your view widgets are large enough to be clearly visible to someone
189    sitting 10 feet away from the screen (this distance is greater for very large screens). The
190    best way to do this is to use layout-relative sizing rather than absolute sizing, and
191    density-independent pixel (dip) units instead of absolute pixel units. For example, to set the
192    width of a widget, use {@code wrap_content} instead of a pixel measurement, and to set the
193    margin for a widget, use dip values instead of px values.</li>
194</ul>
195
196<p>
197  For more information about density-independent pixels and building layouts to handle larger
198  screen sizes, see <a href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple
199  Screens</a>.
200</p>
201
202<h2 id="density-resources">Manage Layout Resources for TV</h2>
203
204<p>The common high-definition TV display resolutions are 720p, 1080i, and 1080p.
205  Your TV layout should target a screen size of 1920 x 1080 pixels, and then allow the Android
206  system to downscale your layout elements to 720p if necessary. In general, downscaling
207  (removing pixels) does not degrade your layout presentation quality. However, upscaling can
208  cause display artifacts that degrade the quality of your layout and have a negative impact on
209  the user experience of your app.</p>
210
211<p>
212  To get the best scaling results for images, provide them as
213  <a href="{@docRoot}tools/help/draw9patch.html">9-patch image</a> elements if possible. If you
214  provide low quality or small images in your layouts, they will appear pixelated, fuzzy, or
215  grainy, which is not a good experience for the user. Use high-quality images instead.
216</p>
217
218<p>
219  For more information on optimizing layouts and resources for large screens see
220  <a href="{@docRoot}training/multiscreen/index.html">Designing for multiple screens</a>.
221</p>
222
223
224<h2 id="anti-patterns">Avoid Layout Anti-Patterns</h2>
225
226<p>
227  There are a few approaches to building layouts that you should avoid because they do not work
228  well on TV devices and lead to bad user experiences. Here are some user interface approaches you
229  should specifically <em>not</em> use when developing a layout for TV.
230</p>
231
232<ul>
233  <li><strong>Re-using phone or tablet layouts</strong> - Do not reuse layouts from a phone or
234    tablet app without modification. Layouts built for other Android device form factors are not
235    well suited for TV devices and should be simplified for operation on a TV.</li>
236  <li><strong>ActionBar</strong> - While this user interface convention is recommended for use
237    on phones and tablets, it is not appropriate for a TV interface. In particular, using an
238    action bar options menu (or any pull-down menu for that matter) is strongly discouraged, due
239    to the difficulty in navigating such a menu with a remote control.</li>
240  <li><strong>ViewPager</strong> - Sliding between screens can work great on a phone or tablet,
241    but don't try this on a TV!</li>
242</ul>
243
244<p>For more information on designing layouts that are appropriate to TV, see the
245  <a href="{@docRoot}design/tv/index.html">TV Design</a> guide.</p>
246
247
248<h2 id="large-bitmaps">Handle Large Bitmaps</h2>
249
250<p>TV devices, like any other Android device, have a limited amount of memory. If you build your
251  app layout with very high-resolution images or use many high-resolution images in the operation
252  of your app, it can quickly run into memory limits and cause out of memory errors.
253  To avoid these types of problems, follow these tips:</p>
254
255<ul>
256  <li>Load images only when they are displayed on the screen. For example, when displaying multiple
257  images in a {@link android.widget.GridView} or {@link android.widget.Gallery}, only load an image
258  when {@link android.widget.Adapter#getView getView()} is called on the
259  view's {@link android.widget.Adapter}.
260  </li>
261  <li>Call {@link android.graphics.Bitmap#recycle()} on {@link android.graphics.Bitmap} views that
262  are no longer needed.
263  </li>
264  <li>Use {@link java.lang.ref.WeakReference} for storing references to {@link
265  android.graphics.Bitmap} objects in an in-memory {@link java.util.Collection}.
266  </li>
267  <li>If you fetch images from the network, use {@link android.os.AsyncTask} to fetch and store
268  them on the device for faster access. Never do network transactions on the application's main
269  user interface thread.
270  </li>
271  <li>Scale down large images to a more appropriate size as you download them; otherwise,
272  downloading the image itself may cause an out of memory exception.
273  </li>
274</ul>
275
276<p>
277  For more information on getting the best performance when working with images, see
278  <a href="{@docRoot}training/displaying-bitmaps/index.html">Displaying Bitmaps Efficiently</a>.
279</p>
280
281<h2 id="advertising">Provide Effective Advertising</h2>
282
283<p>For the living room environment, we recommend you use video ads solutions
284that are full-screen and dismissable within 30 seconds. Functionality for
285advertising on Android TV, such as dismiss buttons and clickthroughs, must be
286accessible using the D-pad rather than touch.</p>
287
288<p>Android TV does not provide a web browser. Your ads must not attempt to
289launch a web browser or redirect to Google Play Store content that is not
290approved for Android TV devices.</p>
291
292<p class="note">
293  <strong>Note:</strong> You can use the {@link android.webkit.WebView} class
294  for logins to services like Google+ and Facebook.
295</p>