1 page.title=Creating TV Navigation 2 page.tags="focus","selection","d-pad" 3 trainingnavtop=true 4 5 @jd:body 6 7 <div id="tb-wrapper"> 8 <div id="tb"> 9 <h2>This lesson teaches you how to</h2> 10 <ol> 11 <li><a href="#d-pad-navigation">Enable D-pad Navigation</a></li> 12 <li><a href="#focus-selection">Provide Clear Focus and Selection</a></li> 13 </ol> 14 15 </div> 16 </div> 17 18 <p> 19 TV devices provide a limited set of navigation controls for apps. Creating an effective 20 navigation scheme for your TV app depends on understanding these limited controls and the limits 21 of users' perception while operating your app. As you build your Android app for TVs, 22 pay special attention to how the user actually navigates around your app when using remote 23 control buttons instead of a touch screen. 24 </p> 25 26 <p> 27 This lesson explains the minimum requirements for creating effective TV app navigation scheme and 28 how to apply those requirements to your app. 29 </p> 30 31 32 <h2 id="d-pad-navigation">Enable D-pad Navigation</h2> 33 34 <p> 35 On a TV device, users navigate with controls on a remote control device, using either a 36 directional pad (D-pad) or arrow keys. This type of control limits movement to up, down, left, 37 and right. To build a great TV-optimized app, you must provide a navigation scheme where the user 38 can quickly learn how to navigate your app using these limited controls. 39 </p> 40 41 <p> 42 The Android framework handles directional navigation between layout elements automatically, so 43 you typically do not need to do anything extra for your app. However, you should thoroughly test 44 navigation with a D-pad controller to discover any navigation problems. Follow these guidelines to 45 test that your app's navigation system works well with a D-pad on a TV device: 46 </p> 47 48 <ul> 49 <li>Ensure that a user with a D-pad controller can navigate to all visible controls on the 50 screen. 51 </li> 52 <li>For scrolling lists with focus, make sure that the D-pad up and down keys scroll the list, 53 and the Enter key selects an item in the list. Verify that users can select an element in the 54 list and that the list still scrolls when an element is selected. 55 </li> 56 <li>Ensure that switching between controls between controls is straightforward and predictable. 57 </li> 58 </ul> 59 60 61 <h3 id="modify-d-pad-nav">Modifying directional navigation</h3> 62 63 <p> 64 The Android framework automatically applies a directional navigation scheme based on the 65 relative position of focusable elements in your layouts. You should test the generated 66 navigation scheme in your app using a D-pad controller. After testing, if you decide you want 67 users to move through your layouts in a specific way, you can set up explicit directional 68 navigation for your controls. 69 </p> 70 71 <p class="note"> 72 <strong>Note:</strong> You should only use these attributes to modify the navigation order if the 73 default order that the system applies does not work well. 74 </p> 75 76 <p> 77 The following code sample shows how to define the next control to receive focus for a {@link 78 android.widget.TextView} layout object: 79 </p> 80 81 <pre> 82 <TextView android:id="@+id/Category1" 83 android:nextFocusDown="@+id/Category2"\> 84 </pre> 85 86 <p> 87 The following table lists all of the available navigation attributes for Android user interface 88 widgets: 89 </p> 90 91 <table> 92 <tr> 93 <th>Attribute</th> 94 <th>Function</th> 95 </tr> 96 <tr> 97 <td>{@link android.R.attr#nextFocusDown}</td> 98 <td>Defines the next view to receive focus when the user navigates down.</td> 99 </tr> 100 <tr> 101 <td>{@link android.R.attr#nextFocusLeft}</td> 102 <td>Defines the next view to receive focus when the user navigates left.</td> 103 </tr> 104 <tr> 105 <td>{@link android.R.attr#nextFocusRight}</td> 106 <td>Defines the next view to receive focus when the user navigates right.</td> 107 </tr> 108 <tr> 109 <td>{@link android.R.attr#nextFocusUp}</td> 110 <td>Defines the next view to receive focus when the user navigates up.</td> 111 </tr> 112 </table> 113 114 <p> 115 To use one of these explicit navigation attributes, set the value to the ID ({@code android:id} 116 value) of another widget in the layout. You should set up the navigation order as a loop, so that 117 the last control directs focus back to the first one. 118 </p> 119 120 121 122 <h2 id="focus-selection">Provide Clear Focus and Selection</h2> 123 124 <p> 125 The success of an app's navigation scheme on TV devices is depends on how easy it is for 126 a user to determine what user interface element is in focus on screen. If you do not provide 127 clear indications of focused items (and therefore what item a user can take action on), they can 128 quickly become frustrated and exit your app. For the same reason, it is important to always have 129 an item in focus that a user can take action on immediately after your app starts, or any time 130 it is idle. 131 </p> 132 133 <p> 134 Your app layout and implementation should use color, size, animation, or a combination of these 135 attributes to help users easily determine what actions they can take next. Use a uniform scheme 136 for indicating focus across your application. 137 </p> 138 139 <p> 140 Android provides <a href="{@docRoot}guide/topics/resources/drawable-resource.html#StateList"> 141 Drawable State List Resources</a> to implement highlights for focused and selected controls. The 142 following code example demonstrates how to enable visual behavior for a button to indicate that a 143 user has navigated to the control and then selected it: 144 </p> 145 146 <pre> 147 <!-- res/drawable/button.xml --> 148 <?xml version="1.0" encoding="utf-8"?> 149 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 150 <item android:state_pressed="true" 151 android:drawable="@drawable/button_pressed" /> <!-- pressed --> 152 <item android:state_focused="true" 153 android:drawable="@drawable/button_focused" /> <!-- focused --> 154 <item android:state_hovered="true" 155 android:drawable="@drawable/button_focused" /> <!-- hovered --> 156 <item android:drawable="@drawable/button_normal" /> <!-- default --> 157 </selector> 158 </pre> 159 160 <p> 161 The following layout XML sample code applies the previous state list drawable to a 162 {@link android.widget.Button}: 163 </p> 164 165 <pre> 166 <Button 167 android:layout_height="wrap_content" 168 android:layout_width="wrap_content" 169 android:background="@drawable/button" /> 170 </pre> 171 172 <p> 173 Make sure to provide sufficient padding within the focusable and selectable controls so that the 174 highlights around them are clearly visible. 175 </p> 176 177 <p> 178 For more recommendations on designing effective selection and focus for your TV app, see 179 <a href="{@docRoot}design/tv/patterns.html">Patterns for TV</a>. 180 </p> 181