2014-09-26 13:00:34 -07:00
|
|
|
page.title=Creating Cards
|
|
|
|
|
|
|
|
@jd:body
|
|
|
|
|
|
|
|
<div id="tb-wrapper">
|
|
|
|
<div id="tb">
|
|
|
|
<h2>This lesson teaches you to</h2>
|
|
|
|
<ol>
|
|
|
|
<li><a href="#card-fragment">Create a Card Fragment</a></li>
|
|
|
|
<li><a href="#card-layout">Add a Card to Your Layout</a></li>
|
|
|
|
</ol>
|
|
|
|
<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>Cards present information to users with a consistent look and feel across different apps.
|
|
|
|
This lesson shows you how to create cards in your Android Wear apps.</p>
|
|
|
|
|
|
|
|
<p>The Wearable UI Library provides implementations of cards specifically designed for wearable
|
2015-06-08 15:05:58 -07:00
|
|
|
devices. This library contains the
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFrame.html"><code>CardFrame</code></a>
|
|
|
|
class, which wraps views inside a card-styled frame with a white background, rounded corners, and a
|
|
|
|
light-drop shadow. A
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFrame.html"><code>CardFrame</code></a>
|
|
|
|
instance can only contain one direct child, usually a layout manager, to which
|
2014-09-26 13:00:34 -07:00
|
|
|
you can add other views to customize the content inside the card.</p>
|
|
|
|
|
|
|
|
<p>You can add cards to your app in two ways:</p>
|
|
|
|
|
|
|
|
<ul>
|
2015-06-08 15:05:58 -07:00
|
|
|
<li>Use or extend the
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
class.</li>
|
|
|
|
<li>Add a card inside a
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardScrollView.html"><code>CardScrollView</code></a>
|
|
|
|
instance in your layout.</li>
|
2014-09-26 13:00:34 -07:00
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p class="note"><strong>Note:</strong> This lesson shows you how to add cards to Android Wear
|
|
|
|
activities. Android notifications on wearable devices are also displayed as cards. For more
|
|
|
|
information, see <a href="{@docRoot}training/wearables/notifications/index.html">Adding Wearable
|
|
|
|
Features to Notifications</a>.</p>
|
|
|
|
|
|
|
|
|
|
|
|
<h2 id="card-fragment">Create a Card Fragment</h2>
|
|
|
|
|
2015-06-08 15:05:58 -07:00
|
|
|
<p>The
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
class provides a default card layout with a title, a description, and an icon. Use this approach to
|
|
|
|
add cards to your app if the default card layout shown in figure 1 meets your needs.</p>
|
2014-09-26 13:00:34 -07:00
|
|
|
|
|
|
|
<img src="{@docRoot}wear/images/05_uilib.png" width="500" height="245" alt=""/>
|
2015-06-08 15:05:58 -07:00
|
|
|
<p class="img-caption"><strong>Figure 1.</strong> The default
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
layout.</p>
|
2014-09-26 13:00:34 -07:00
|
|
|
|
2015-06-08 15:05:58 -07:00
|
|
|
<p>To add a
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
instance to your app:</p>
|
2014-09-26 13:00:34 -07:00
|
|
|
|
|
|
|
<ol>
|
|
|
|
<li>In your layout, assign an ID to the element that contains the card</li>
|
2015-06-08 15:05:58 -07:00
|
|
|
<li>Create a
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
instance in your activity</li>
|
|
|
|
<li>Use the fragment manager to add the
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
instance to its container</li>
|
2014-09-26 13:00:34 -07:00
|
|
|
</ol>
|
|
|
|
|
2015-06-08 15:05:58 -07:00
|
|
|
<p>The following sample code shows the code for the screen display shown in figure 1:</p>
|
2014-09-26 13:00:34 -07:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
<android.support.wearable.view.BoxInsetLayout
|
|
|
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
|
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
|
|
android:background="@drawable/robot_background"
|
|
|
|
android:layout_height="match_parent"
|
|
|
|
android:layout_width="match_parent">
|
|
|
|
|
|
|
|
<FrameLayout
|
|
|
|
<strong>android:id="@+id/frame_layout"</strong>
|
|
|
|
android:layout_width="match_parent"
|
|
|
|
android:layout_height="match_parent"
|
|
|
|
app:layout_box="bottom">
|
|
|
|
|
|
|
|
</FrameLayout>
|
|
|
|
</android.support.wearable.view.BoxInsetLayout>
|
|
|
|
</pre>
|
|
|
|
|
2015-06-08 15:05:58 -07:00
|
|
|
<p>The following code adds the
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
instance to the activity in figure 1:</p>
|
2014-09-26 13:00:34 -07:00
|
|
|
|
|
|
|
<pre>
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_wear_activity2);
|
|
|
|
|
|
|
|
FragmentManager fragmentManager = getFragmentManager();
|
|
|
|
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
|
|
|
CardFragment cardFragment = CardFragment.create(getString(R.string.cftitle),
|
|
|
|
getString(R.string.cfdesc),
|
|
|
|
R.drawable.p);
|
|
|
|
fragmentTransaction.add(R.id.frame_layout, cardFragment);
|
|
|
|
fragmentTransaction.commit();
|
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
|
2015-06-08 15:05:58 -07:00
|
|
|
<p>To create a card with a custom layout using the
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html"><code>CardFragment</code></a>
|
|
|
|
class, extend the class and override its
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFragment.html#onCreateContentView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)"><code>onCreateContentView</code></a>
|
|
|
|
method.</p>
|
2014-09-26 13:00:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
<h2 id="card-layout">Add a CardFrame to Your Layout</h2>
|
|
|
|
|
|
|
|
<p>You can also add a card directly to your layout definition, as shown in figure 2. Use this
|
|
|
|
approach when you want to define a custom layout for the card inside a layout definition file.</p>
|
|
|
|
|
|
|
|
<img src="{@docRoot}wear/images/04_uilib.png" width="500" height="248" alt=""/>
|
2015-06-08 15:05:58 -07:00
|
|
|
<p class="img-caption"><strong>Figure 2.</strong> Adding a
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardFrame.html"><code>CardFrame</code></a>
|
|
|
|
to your layout.</p>
|
2014-09-26 13:00:34 -07:00
|
|
|
|
|
|
|
<p>The following layout code sample demonstrates a vertical linear layout with two elements. You
|
|
|
|
can create more complex layouts to fit the needs of your app.</p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
<android.support.wearable.view.BoxInsetLayout
|
|
|
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|
|
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
|
|
android:background="@drawable/robot_background"
|
|
|
|
android:layout_height="match_parent"
|
|
|
|
android:layout_width="match_parent">
|
|
|
|
|
|
|
|
<<strong>android.support.wearable.view.CardScrollView</strong>
|
|
|
|
android:id="@+id/card_scroll_view"
|
|
|
|
android:layout_height="match_parent"
|
|
|
|
android:layout_width="match_parent"
|
|
|
|
app:layout_box="bottom">
|
|
|
|
|
|
|
|
<<strong>android.support.wearable.view.CardFrame</strong>
|
|
|
|
android:layout_height="wrap_content"
|
|
|
|
android:layout_width="fill_parent">
|
|
|
|
|
|
|
|
<LinearLayout
|
|
|
|
android:layout_height="wrap_content"
|
|
|
|
android:layout_width="match_parent"
|
|
|
|
android:orientation="vertical"
|
|
|
|
android:paddingLeft="5dp">
|
|
|
|
<TextView
|
|
|
|
android:fontFamily="sans-serif-light"
|
|
|
|
android:layout_height="wrap_content"
|
|
|
|
android:layout_width="match_parent"
|
|
|
|
android:text="@string/custom_card"
|
|
|
|
android:textColor="@color/black"
|
|
|
|
android:textSize="20sp"/>
|
|
|
|
<TextView
|
|
|
|
android:fontFamily="sans-serif-light"
|
|
|
|
android:layout_height="wrap_content"
|
|
|
|
android:layout_width="match_parent"
|
|
|
|
android:text="@string/description"
|
|
|
|
android:textColor="@color/black"
|
|
|
|
android:textSize="14sp"/>
|
|
|
|
</LinearLayout>
|
|
|
|
</android.support.wearable.view.CardFrame>
|
|
|
|
</android.support.wearable.view.CardScrollView>
|
|
|
|
</android.support.wearable.view.BoxInsetLayout>
|
|
|
|
</pre>
|
|
|
|
|
2015-06-08 15:05:58 -07:00
|
|
|
<p>The
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardScrollView.html><code><CardScrollView></code></a>
|
|
|
|
element in the example layout above lets you assign gravity to
|
2014-09-26 13:00:34 -07:00
|
|
|
the card when its content is smaller than the container. This example aligns the card to the
|
|
|
|
bottom of the screen:</p>
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_wear_activity2);
|
|
|
|
|
|
|
|
CardScrollView cardScrollView =
|
|
|
|
(CardScrollView) findViewById(R.id.card_scroll_view);
|
|
|
|
cardScrollView.setCardGravity(Gravity.BOTTOM);
|
|
|
|
}
|
|
|
|
</pre>
|
2014-10-03 16:43:25 -07:00
|
|
|
|
2015-06-08 15:05:58 -07:00
|
|
|
<p>The
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/CardScrollView.html"><code><CardScrollView></code></a>
|
|
|
|
element detects the shape of the screen and displays the card differently
|
2014-10-03 16:43:25 -07:00
|
|
|
on round and square devices, using wider side margins on round screens. However,
|
2015-06-08 15:05:58 -07:00
|
|
|
placing the
|
|
|
|
<a href="{@docRoot}reference/android/supprot/wearable/view/CardScrollView.html"><code><CardScrollView></code></a>
|
|
|
|
element inside a
|
|
|
|
<a href="{@docRoot}reference/android/support/wearable/view/BoxInsetLayout.html"><code><BoxInsetLayout></code></a>
|
|
|
|
and using the <code>layout_box="bottom"</code> attribute is useful to align the card to the bottom
|
|
|
|
of round screens without cropping its content.</p>
|