Skip to content

Commit 359c78a

Browse files
labtopiaAndroid Git Automerger
authored andcommitted
am f887b56: Merge "Doc update: Appwidgets MR1 lockscreen support." into jb-mr1-dev
* commit 'f887b5631e6bd0fad3505f0be28eaab5e61b0d74': Doc update: Appwidgets MR1 lockscreen support.
2 parents b70d528 + f887b56 commit 359c78a

File tree

1 file changed

+84
-3
lines changed
  • docs/html/guide/topics/appwidgets

1 file changed

+84
-3
lines changed

docs/html/guide/topics/appwidgets/index.jd

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ page.title=App Widgets
22
@jd:body
33

44
<div id="qv-wrapper">
5-
<div id="qv">
5+
<div id="qv">re
66
<h2>Quickview</h2>
77
<ul>
88
<li>App Widgets provide users access to some of your application features
@@ -33,6 +33,11 @@ from
3333
</ol>
3434
</li>
3535
<li><a href="#preview">Setting a Preview Image</a></li>
36+
<li><a href="#lockscreen">Enabling App Widgets on the Lockscreen
37+
<ol>
38+
<li><a href="#lockscreen-sizing">Sizing guidelines</li>
39+
</ol>
40+
</li>
3641
<li><a href="#collections">Using App Widgets with Collections</a>
3742
<ol>
3843
<li><a href="#collection_sample">Sample application</a></li>
@@ -179,7 +184,9 @@ folder.</p>
179184
android:previewImage="@drawable/preview"
180185
android:initialLayout="@layout/example_appwidget"
181186
android:configure="com.example.android.ExampleAppWidgetConfigure"
182-
android:resizeMode="horizontal|vertical">
187+
android:resizeMode="horizontal|vertical"
188+
android:widgetCategory="home_screen|keyguard"
189+
android:initialKeyguardLayout="@layout/example_keyguard">
183190
&lt;/appwidget-provider>
184191
</pre>
185192

@@ -274,7 +281,21 @@ widget to show its resize handles, then drag the horizontal and/or vertical
274281
handles to change the size on the layout grid. Values for the
275282
<code>resizeMode</code> attribute include "horizontal", "vertical", and "none".
276283
To declare a widget as resizeable horizontally and vertically, supply the value
277-
"horizontal|vertical". Introduced in Android 3.1.</li> </ul>
284+
"horizontal|vertical". Introduced in Android 3.1.</li>
285+
286+
<li>The <code>widgetCategory</code> attribute declares whether your App Widget can be displayed on the home screen,
287+
the lock screen (keyguard), or both. Values for this attribute include "home_screen" and "keyguard". A widget that
288+
is displayed on both needs to ensure that it follows the design guidelines for both widget classes. For more
289+
information, see <a href="#lockscreen">Enabling App Widgets on the Lockscreen</a>. The default value is "home_screen". Introduced in Android 4.2.
290+
</li>
291+
292+
<li>The <code>initialKeyguardLayout</code> attribute points to the layout resource
293+
that defines the lock screen App Widget layout. This works the same way as the
294+
{@link android.appwidget.AppWidgetProviderInfo#initialLayout android:initialLayout},
295+
in that it provides a layout that can appear immediately until your app widget is initialized and able to update
296+
the layout. Introduced in Android 4.2.</li>
297+
298+
</ul>
278299

279300
<p>See the {@link android.appwidget.AppWidgetProviderInfo} class for more
280301
information on the
@@ -731,6 +752,66 @@ preview image, launch this application, select the app widget for your
731752
application and set it up how you'd like your preview image to appear, then save
732753
it and place it in your application's drawable resources.</p>
733754

755+
<h2 id="lockscreen">Enabling App Widgets on the Lockscreen</h2>
756+
757+
<p>Android 4.2 introduces the ability for users to add widgets to the lock screen. To indicate that your app widget is available for use on the lock screen, declare the {@link android.appwidget.AppWidgetProviderInfo#widgetCategory android:widgetCategory} attribute in the XML file that specifies your {@link android.appwidget.AppWidgetProviderInfo}. This attribute supports two values: "home_screen" and "keyguard". An app widget can declare support for one or both.</p>
758+
759+
<p>By default, every app widget supports placement on the Home screen, so "home_screen" is the default value for the
760+
{@link android.appwidget.AppWidgetProviderInfo#widgetCategory android:widgetCategory} attribute. If you want your app widget to be available for the lock screen, add the "keyguard" value:</p>
761+
<pre>
762+
&lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
763+
...
764+
android:widgetCategory="keyguard|home_screen">
765+
&lt;/appwidget-provider>
766+
</pre>
767+
768+
<p>If you declare a widget to be displayable on both keyguard (lockscreen) and home, it's likely that you'll want to customize the widget depending on where it is displayed. For example, you might create a separate layout file for keyguard vs. home. The next step is to detect the widget category at runtime and respond accordingly.
769+
770+
You can detect whether your widget is on the lockscreen or home screen by calling
771+
{@link android.appwidget.AppWidgetManager#getAppWidgetOptions getAppWidgetOptions()}
772+
to get the widget's options as a {@link android.os.Bundle}. The returned bundle will include the key
773+
{@link android.appwidget.AppWidgetManager#OPTION_APPWIDGET_HOST_CATEGORY}, whose value will be one of {@link android.appwidget.AppWidgetProviderInfo#WIDGET_CATEGORY_HOME_SCREEN} or
774+
{@link android.appwidget.AppWidgetProviderInfo#WIDGET_CATEGORY_KEYGUARD}. This value is determined by the host into which the widget is bound. In the {@link android.appwidget.AppWidgetProvider}, you can then check the widget's category, for example:</p>
775+
776+
<pre>
777+
AppWidgetManager appWidgetManager;
778+
int widgetId;
779+
Bundle myOptions = appWidgetManager.getAppWidgetOptions (widgetId);
780+
781+
// Get the value of OPTION_APPWIDGET_HOST_CATEGORY
782+
int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
783+
784+
// If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
785+
boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
786+
</pre>
787+
788+
<p>Once you know the widget's category, you can optionally load a different base layout, set different properties, and so on. For example:</p>
789+
790+
<pre>
791+
int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
792+
</pre>
793+
794+
795+
<p>You should also specify an initial layout for your app widget when on the lock screen with the
796+
{@link android.appwidget.AppWidgetProviderInfo#initialKeyguardLayout android:initialKeyguardLayout} attribute. This works the same way as the
797+
{@link android.appwidget.AppWidgetProviderInfo#initialLayout android:initialLayout}, in that it provides a layout that can appear immediately until your app widget is initialized and able to update the layout.</p>
798+
799+
<h3 id="lockscreen-sizing">Sizing guidelines</h3>
800+
801+
<p>When a widget is hosted on the lockscreen, the framework ignores the {@code minWidth}, {@code minHeight}, {@code minResizeWidth}, and {@code minResizeHeight} fields. If a widget is also a home screen widget, these parameters are still needed as they're still used on home, but they will be ignored for purposes of the lockscreen.</p>
802+
803+
<p>The width of a lockscreen widget always fills the provided space. For the height of a lockscreen widget, you have the following options:</p>
804+
805+
<ul>
806+
<li>If the widget does not mark itself as vertically resizable ({@code android:resizeMode="vertical"}), then the widget height will always be "small":
807+
<ul>
808+
<li>On a phone in portrait mode, "small" is defined as the space remaining when an unlock UI is being displayed.</li>
809+
<li>On tablets and landscape phones, "small" is set on a per-device basis.</li>
810+
</ul>
811+
</li>
812+
<li>If the widget marks itself as vertically resizable, then the widget height shows up as "small" on portrait phones displaying an unlock UI. In all other cases, the widget sizes to fill the available height.</li>
813+
</ul>
814+
734815
<h2 id="collections">Using App Widgets with Collections</h2>
735816

736817
<p>Android 3.0 introduces App Widgets with collections. These kinds of App

0 commit comments

Comments
 (0)