132 lines
5.7 KiB
Plaintext
132 lines
5.7 KiB
Plaintext
page.title=Creating Custom Layouts
|
|
|
|
@jd:body
|
|
|
|
<div id="tb-wrapper">
|
|
<div id="tb">
|
|
|
|
<h2>This lesson teaches you to</h2>
|
|
<ol>
|
|
<li><a href="#CustomNotifications">Create custom notifications</a></li>
|
|
<li><a href="#UiLibrary">Create Layouts with the Wearable UI Library</li>
|
|
</ol>
|
|
|
|
<!--STOPSHIP: link these -->
|
|
<h2>You should also read</h2>
|
|
<ul>
|
|
<li><a href="{@docRoot}design/wear/index.html">Android Wear Design Principles</a></li>
|
|
</ul>
|
|
|
|
</div>
|
|
</div>
|
|
|
|
<p>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 <a href="{@docRoot}design/wear/index.html">design guidelines</a>
|
|
for information on how to design great wearable apps.</p>
|
|
|
|
<h2 id="CustomNotifications">Create Custom Notifications</h2>
|
|
|
|
<p>
|
|
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.</p>
|
|
|
|
<p>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.</p>
|
|
|
|
<p clas="note"><b>Note:</b> When creating custom notifications on the wearable, you can use the
|
|
standard notification APIs (API Level 20) instead of the Support Library.
|
|
</p>
|
|
|
|
<p>To create a custom notification:</p>
|
|
<ol>
|
|
<li>Create a layout and set it as the content view for the activity
|
|
that you want to display.
|
|
<pre>
|
|
public void onCreate(Bundle bundle){
|
|
...
|
|
setContentView(R.layout.notification_activity);
|
|
}
|
|
</pre>
|
|
</li>
|
|
<li>Set the activity's <code>allowEmbedded</code> property to <code>true</code>
|
|
in the wearable app's Android manifest. This allows an activity that you create in your wearable
|
|
app to be displayed by the wearable's context stream process.</li>
|
|
<pre>
|
|
<activity android:allowEmbedded="true" ... >
|
|
...
|
|
</activity>
|
|
</pre>
|
|
</li>
|
|
<li>Create a {@link android.app.PendingIntent} for the activity that you want to display.
|
|
For example:
|
|
<pre>
|
|
Intent notificationIntent = new Intent(this, NotificationActivity.class);
|
|
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
|
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
</pre>
|
|
</li>
|
|
<li>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.
|
|
</li>
|
|
<li>Issue the notification using the
|
|
{@link android.app.NotificationManager#notify notify()} method.
|
|
<p class="note"><b>Note:</b> The system initially displays your notification with
|
|
a standard template. This template works well on all watchfaces. When users swipe the notification
|
|
up to view it, they'll then see your activity for the notification.</p>
|
|
</li>
|
|
</ol>
|
|
<h2 id="UiLibrary">Create Layouts with the Wearable UI Library</h2>
|
|
<p>
|
|
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 <code>build.gradle</code>
|
|
file with the following dependency declaration:
|
|
|
|
<pre>
|
|
dependencies {
|
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
|
<b>compile 'com.google.android.support:wearable:+'</b>
|
|
compile 'com.google.android.gms:play-services-wearable:+'
|
|
}
|
|
</pre>
|
|
This library helps you build UIs that are designed for wearables. Here are some of the major classes:
|
|
</p>
|
|
|
|
<ul>
|
|
<li><code>BoxInsetLayout</code> - A FrameLayout that's aware of screen shape and can box its
|
|
children in the center square of a round screen.</li>
|
|
<li><code>CardFragment</code> - A fragment that presents content within an expandable,
|
|
vertically scrollable card.</li>
|
|
<li><code>CircledImageView</code> - An image view surrounded by a circle.</li>
|
|
<li><code>ConfirmationActivity</code> - An activity that displays confirmation animations after the user
|
|
completes an action.</li>
|
|
<li><code>DismissOverlayView</code> - A view for implementing long-press-to-dismiss.</li>
|
|
<li><code>GridViewPager</code> - A layout manager that allows the user to both vertically and
|
|
horizontally through pages of data. You supply an implementation of a GridPagerAdapter to
|
|
generate the pages that the view shows.</li>
|
|
<li><code>GridPagerAdapter</code> - An adapter that supplies pages to a GridViewPager.</li>
|
|
<li><code>FragmentGridPagerAdapter</code> - An implementation of GridPagerAdapter that
|
|
represents each page as a fragment.</li>
|
|
</li>
|
|
<li><code>WatchViewStub</code> - A class that can inflate a specific layout,
|
|
depending on the shape of the device's screen.</li>
|
|
<li><code>WearableListView</code> - An alternative version of ListView that is optimized for
|
|
ease of use on small screen wearable devices. It displays a vertically scrollable list of items,
|
|
and automatically snaps to the nearest item when the user stops scrolling.
|
|
</li>
|
|
</ul>
|
|
|
|
<p class="note"><a href="{@docRoot}shareables/training/wearable-support-docs.zip">Download the full API
|
|
reference documentation</a> for the classes above. The documentation goes over how to use
|
|
each UI widget.</p>
|