1page.title=Creating Custom Layouts
2page.tags=wear
3helpoutsWidget=true
4
5@jd:body
6
7<div id="tb-wrapper">
8<div id="tb">
9
10<h2>This lesson teaches you to</h2>
11<ol>
12  <li><a href="#CustomNotifications">Create custom notifications</a></li>
13  <li><a href="#UiLibrary">Create Layouts with the Wearable UI Library</a></li>
14</ol>
15
16<h2>You should also read</h2>
17<ul>
18  <li><a href="{@docRoot}design/wear/index.html">Android Wear Design Principles</a></li>
19</ul>
20
21</div>
22</div>
23
24<p>Creating layouts for wearables is the same as handheld devices, except you have to design
25for the screen size and for glanceability. Do not port functionality
26and the UI from a handheld app and expect a good experience. You should create custom layouts
27only when necessary. Read the <a href="{@docRoot}design/wear/index.html">design guidelines</a>
28for information on how to design great wearable apps.</p>
29
30<h2 id="CustomNotifications">Create Custom Notifications</h2>
31
32<p>
33In general, you should create notifications on the handheld and let them
34automatically sync to the wearable. This lets you build your notifications
35once and have them appear on many types of devices (not just wearables, but
36eventually Auto and TV) without having to design them for different
37form factors.</p>
38
39<p>If the standard notification styles don't work for you (such as
40{@link android.support.v4.app.NotificationCompat.BigTextStyle} or
41{@link android.support.v4.app.NotificationCompat.InboxStyle}), you can display an activity with
42a custom layout. You can only create and issue custom notifications on the wearable, and the
43system does not sync these notifications to the handheld.</p>
44
45<p clas="note"><b>Note:</b> When creating custom notifications on the wearable, you can use the
46standard notification APIs (API Level 20) instead of the Support Library.
47</p>
48
49<p>To create a custom notification:</p>
50<ol>
51  <li>Create a layout and set it as the content view for the activity
52  that you want to display.
53<pre>
54public void onCreate(Bundle bundle){
55    ...
56    setContentView(R.layout.notification_activity);
57}
58</pre>
59  </li>
60  <li>Define necessary properties for the activity in the Android manifest to allow
61  the activity to be displayed in the wearable's context stream process. You need to declare the
62  activity to be exportable, be embeddable, and have an empty task affinity. We also recommend
63  setting the theme to <code>Theme.DeviceDefault.Light</code>. For example:</li>
64<pre>
65&lt;activity android:name="com.example.MyDisplayActivity"
66     android:exported="true"
67     android:allowEmbedded="true"
68     android:taskAffinity=""
69     android:theme="@android:style/Theme.DeviceDefault.Light" /&gt;
70</pre>
71  </li>
72  <li>Create a {@link android.app.PendingIntent} for the activity that you want to display.
73  For example:
74<pre>
75Intent notificationIntent = new Intent(this, NotificationActivity.class);
76PendingIntent notificationPendingIntent = PendingIntent.getActivity(
77        this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
78</pre>
79  </li>
80  <li>Build a {@link android.app.Notification} and call
81  {@link android.app.Notification.WearableExtender#setDisplayIntent setDisplayIntent()}
82  providing the {@link android.app.PendingIntent}. The system uses this
83  {@link android.app.PendingIntent} to launch the activity when
84  users view your notification.
85  </li>
86  <li>Issue the notification using the
87  <a href="{@docRoot}reference/android/app/NotificationManager.html#notify(int, android.app.Notification)"><code>notify()</code></a> method.
88  <p class="note"><b>Note:</b> When the notification is peeking on the homescreen, the system
89  displays it with a standard template that it generates from the notification's semantic data. This template works well on all watchfaces. When users swipe the notification up, they'll then see the
90  custom activity for the notification.</p>
91  </li>
92</ol>
93<h2 id="UiLibrary">Create Layouts with the Wearable UI Library</h2>
94<p>
95The Wearable UI Library is automatically included when you create your wearable
96app with the Android Studio Project Wizard. You can also add this library to your <code>build.gradle</code>
97file with the following dependency declaration:
98</p>
99
100<pre>
101dependencies {
102    compile fileTree(dir: 'libs', include: ['*.jar'])
103    <b>compile 'com.google.android.support:wearable:+'</b>
104    compile 'com.google.android.gms:play-services-wearable:+'
105}
106</pre>
107
108<p>This library helps you build UIs that are designed for wearables. For more information, see
109<a href="{@docRoot}training/wearables/ui/index.html">Creating Custom UIs for Wear Devices</a>.</p>
110
111<p>Here are some of the major classes in the Wearable UI Library:</p>
112
113<dl>
114  <dt><a href="{@docRoot}reference/android/support/wearable/view/BoxInsetLayout.html"><code>BoxInsetLayout</code></a>
115  </dt>
116  <dd>A {@link android.widget.FrameLayout} object
117  that's aware of screen shape and can box its children in the center square of a round screen.
118  </dd>
119
120  <dt><a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
121  </dt>
122  <dd>A fragment that presents content within an expandable, vertically scrollable card.
123  </dd>
124
125  <dt><a href="{@docRoot}reference/android/support/wearable/view/CircledImageView.html"><code>CircledImageView</code></a>
126  </dt>
127  <dd>An image view surrounded by a circle.
128  </dd>
129
130  <dt><a href="{@docRoot}reference/android/support/wearable/activity/ConfirmationActivity.html"><code>ConfirmationActivity</code></a>
131  </dt>
132  <dd>An activity that displays confirmation animations after the user completes an action.
133  </dd>
134
135  <dt><a href="{@docRoot}reference/android/support/wearable/view/CrossfadeDrawable.html"><code>CrossFadeDrawable</code></a>
136  </dt>
137  <dd>A drawable that contains two child drawables and provides methods to directly adjust the blend
138  between the two.
139  </dd>
140
141  <dt><a href="{@docRoot}reference/android/support/wearable/view/DelayedConfirmationView.html"><code>DelayedConfirmationView</code></a>
142  </dt>
143  <dd>A view that provides a circular countdown timer, typically used to automatically confirm an
144  operation after a short delay has elapsed.
145  </dd>
146
147  <dt><a href="{@docRoot}reference/android/support/wearable/view/DismissOverlayView.html"><code>DismissOverlayView</code></a>
148  </dt>
149  <dd>A view for implementing long-press-to-dismiss.
150  </dd>
151
152  <dt><a href="{@docRoot}reference/android/support/wearable/view/GridViewPager.html"><code>GridViewPager</code></a>
153  </dt>
154  <dd>A layout manager that allows the user to navigate both vertically and
155    horizontally through pages of data. You supply an implementation of a
156    <a href="{@docRoot}reference/android/support/wearable/view/GridPagerAdapter.html"><code>GridPagerAdapter</code></a>
157    instance to generate the pages that the view shows.
158  </dd>
159
160  <dt><a href="{@docRoot}reference/android/support/wearable/view/GridPagerAdapter.html"><code>GridPagerAdapter</code></a>
161  </dt>
162  <dd>An adapter that supplies pages to a
163  <a href="{@docRoot}reference/android/support/wearable/view/GridViewPager.html"><code>GridViewPager</code></a>
164  object.
165  </dd>
166
167  <dt><a href="{@docRoot}reference/android/support/wearable/view/FragmentGridPagerAdapter.html"><code>FragmentGridPagerAdapter</code></a>
168  </dt>
169  <dd>An implementation of a
170  <a href="{@docRoot}reference/android/support/wearable/view/GridPagerAdapter.html"><code>GridPagerAdapter</code></a>
171  instance that represents each page as a fragment.
172  </dd>
173
174  <dt><a href="{@docRoot}reference/android/support/wearable/view/DotsPageIndicator.html"><code>DotsPageIndicator</code></a>
175  </dt>
176  <dd>A page indicator for a
177  <a href="{@docRoot}reference/android/support/wearable/view/GridViewPager.html"><code>GridViewPager</code></a>
178  implementation that identifies the current page in relation to all available pages on the current
179  row.
180  </dd>
181
182  <dt><a href="{@docRoot}reference/android/support/wearable/view/WatchViewStub.html"><code>WatchViewStub</code></a>
183  </dt>
184  <dd>A class that can inflate a specific layout, depending on the shape of the device's screen.
185  </dd>
186
187  <dt><a href="{@docRoot}reference/android/support/wearable/view/WearableListView.html"><code>WearableListView</code></a>
188  </dt>
189  <dd>An alternative version of a {@link android.widget.ListView}
190  object that is optimized for ease of use on small screen wearable devices. It displays a
191  vertically scrollable list of items, and automatically snaps to the nearest item when the user
192  stops scrolling.
193  </dd>
194</dl>
195
196<h3 id="UiLibReference">Wear UI library API reference</h3>
197
198<p>The reference documentation explains how to use each UI widget in detail. Browse the
199<a href="{@docRoot}reference/android/support/wearable/view/package-summary.html">Wear API
200reference documentation</a> for the classes above.</p>
201
202<p class="note"><strong>Note:</strong> We recommend using <a
203href="{@docRoot}studio/index.html">Android Studio</a> for Android Wear development, as
204it provides project setup, library inclusion, and packaging conveniences.</p>
205