Home | History | Annotate | Download | only in apps
      1 page.title=Creating Custom Layouts
      2 page.tags=wear
      3 helpoutsWidget=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</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
     25 for the screen size and for glanceability. Do not port functionality
     26 and the UI from a handheld app and expect a good experience. You should create custom layouts
     27 only when necessary. Read the <a href="{@docRoot}design/wear/index.html">design guidelines</a>
     28 for information on how to design great wearable apps.</p>
     29 
     30 <h2 id="CustomNotifications">Create Custom Notifications</h2>
     31 
     32 <p>
     33 In general, you should create notifications on the handheld and let them
     34 automatically sync to the wearable. This lets you build your notifications
     35 once and have them appear on many types of devices (not just wearables, but
     36 eventually Auto and TV) without having to design them for different
     37 form 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
     42 a custom layout. You can only create and issue custom notifications on the wearable, and the
     43 system 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
     46 standard 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>
     54 public 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>
     75 Intent notificationIntent = new Intent(this, NotificationActivity.class);
     76 PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
     77         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>
     95 The Wearable UI Library is automatically included when you create your wearable
     96 app with the Android Studio Project Wizard. You can also add this library to your <code>build.gradle</code>
     97 file with the following dependency declaration:
     98 </p>
     99 
    100 <pre>
    101 dependencies {
    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 <ul>
    114     <li><code>BoxInsetLayout</code> - A FrameLayout that's aware of screen shape and can box its
    115     children in the center square of a round screen.</li>
    116     <li><code>CardFragment</code> - A fragment that presents content within an expandable,
    117     vertically scrollable card.</li>
    118     <li><code>CircledImageView</code> - An image view surrounded by a circle.</li>
    119     <li><code>ConfirmationActivity</code> - An activity that displays confirmation animations after the user
    120     completes an action.</li>
    121     <li><code>CrossFadeDrawable</code> - A drawable that contains two child drawables and provides
    122     methods to directly adjust the blend between the two.</li>
    123     <li><code>DelayedConfirmationView</code> - A view that provides a circular countdown timer,
    124     typically used to automatically confirm an operation after a short delay has elapsed.</li>
    125     <li><code>DismissOverlayView</code> - A view for implementing long-press-to-dismiss.</li>
    126     <li><code>DotsPageIndicator</code> - A page indicator for GridViewPager that identifies the
    127     current page in relation to all available pages on the current row.</li>
    128     <li><code>GridViewPager</code> - A layout manager that allows the user to both vertically and
    129     horizontally through pages of data. You supply an implementation of a GridPagerAdapter to
    130     generate the pages that the view shows.</li>
    131     <li><code>GridPagerAdapter</code> - An adapter that supplies pages to a GridViewPager.</li>
    132     <li><code>FragmentGridPagerAdapter</code> - An implementation of GridPagerAdapter that
    133     represents each page as a fragment.</li>
    134     </li>
    135     <li><code>WatchViewStub</code> - A class that can inflate a specific layout,
    136     depending on the shape of the device's screen.</li>
    137     <li><code>WearableListView</code> - An alternative version of ListView that is optimized for
    138     ease of use on small screen wearable devices. It displays a vertically scrollable list of items,
    139     and automatically snaps to the nearest item when the user stops scrolling.
    140     </li>
    141 </ul>
    142 
    143 <h3 id="UiLibReference">Wear UI library API reference</h3>
    144 
    145 <p>The reference documentation explains how to use each UI widget in detail. Browse the
    146 <a href="{@docRoot}reference/android/support/wearable/view/package-summary.html">Wear API
    147 reference documentation</a> for the classes above.</p>
    148 
    149 <h3 id="UiLibEclipse">Download the Wearable UI library for Eclipse ADT</h3>
    150 
    151 <p>If you are using the ADT plugin for Eclipse, download the
    152 <a href="{@docRoot}shareables/training/wearable-support-lib.zip">Wearable UI library</a> to
    153 include the Wearable UI library as a dependency in your project.</p>
    154 
    155 <p class="note"><strong>Note:</strong> We recommend
    156 <a href="{@docRoot}sdk/index.html">Android Studio</a> for Android Wear app
    157 development.</p>
    158