page.title=Creating Custom Layouts @jd:body

This lesson teaches you to

  1. Create custom notifications
  2. Create Layouts with the Wearable UI Library

You should also read

Creating layouts for wearables is the same as handheld devices, except you have to design for the screen size and for glanceability. Do not port functionality and the UI from a handheld app and expect a good experience. You should create custom layouts only when necessary. Read the design guidelines for information on how to design great wearable apps.

Create Custom Notifications

In general, you should create notifications on the handheld and let them automatically sync to the wearable. This lets you build your notifications once and have them appear on many types of devices (not just wearables, but eventually Auto and TV) without having to design them for different form factors.

If the standard notification styles don't work for you (such as {@link android.support.v4.app.NotificationCompat.BigTextStyle} or {@link android.support.v4.app.NotificationCompat.InboxStyle}), you can display an activity with a custom layout. You can only create and issue custom notifications on the wearable, and the system does not sync these notifications to the handheld.

Note: When creating custom notifications on the wearable, you can use the standard notification APIs (API Level 20) instead of the Support Library.

To create a custom notification:

  1. Create a layout and set it as the content view for the activity that you want to display.
    public void onCreate(Bundle bundle){
        ...
        setContentView(R.layout.notification_activity);
    }
    
  2. Define necessary properties for the activity in the Android manifest to allow the activity to be displayed in the wearable's context stream process. You need to declare the activity to be exportable, be embeddable, and have an empty task affinity. We also recommend setting the theme to Theme.DeviceDefault.Light. For example:
  3. <activity android:name="com.example.MyDisplayActivity"
         android:exported="true"
         android:allowEmbedded="true"
         android:taskAffinity=""
         android:theme="@android:style/Theme.DeviceDefault.Light" />
    
  4. Create a {@link android.app.PendingIntent} for the activity that you want to display. For example:
    Intent notificationIntent = new Intent(this, NotificationActivity.class);
    PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    
  5. Build a {@link android.app.Notification} and call {@link android.app.Notification.WearableExtender#setDisplayIntent setDisplayIntent()} providing the {@link android.app.PendingIntent}. The system uses this {@link android.app.PendingIntent} to launch the activity when users view your notification.
  6. Issue the notification using the notify() method.

    Note: When the notification is peeking on the homescreen, the system 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 custom activity for the notification.

Create Layouts with the Wearable UI Library

There's an unofficial UI library that is automatically included when you create your wearable app with the Android Studio Project Wizard. You can also add the library to your build.gradle file with the following dependency declaration:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.support:wearable:+'
    compile 'com.google.android.gms:play-services-wearable:+'
}
This library helps you build UIs that are designed for wearables. Here are some of the major classes:

Download the full API reference documentation for the classes above. The documentation goes over how to use each UI widget.