Home | History | Annotate | Download | only in ui_guidelines
      1 page.title=App Widget Design Guidelines
      2 parent.title=UI Guidelines
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7 <div id="qv">
      8 
      9 <h2>Quickview</h2>
     10 
     11 <ul>
     12   <li>App Widget layouts should be flexible, resizing to fit their parent container</li>
     13   <li>As of Android 3.0, app widgets can depict collections of items and provide a representative
     14     preview image for the widget gallery</li>
     15   <li>As of Android 3.1, app widgets can be resizable horizontally and/or vertically</li>
     16   <li>As of Android 4.0, app widgets have margins automatically applied</li>
     17 </ul>
     18 
     19 <h2>In this document</h2>
     20 
     21 <ol>
     22 <li><a href="#anatomy">Standard Widget Anatomy</a></li>
     23 <li><a href="#design">Designing Widget Layouts and Background Graphics</a></li>
     24 <li><a href="#templates">Using the App Widget Templates Pack</a></li>
     25 </ol>
     26 
     27 <h2>Downloads</h2>
     28 
     29 <ol>
     30 <li><a href="{@docRoot}shareables/app_widget_templates-v4.0.zip">App Widget Templates Pack,
     31   v4.0 &raquo;</a></li>
     32 </ol>
     33 
     34 <h2>See also</h2>
     35 
     36 <ol>
     37 <li><a href="{@docRoot}guide/topics/appwidgets/index.html">App Widgets</a></li>
     38 <li>
     39   <a href="http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html">
     40     AppWidgets blog post</a></li>
     41 </ol>
     42 
     43 </div>
     44 </div>
     45 
     46 
     47 <div class="note design">
     48 <p><strong>New Guides for App Designers!</strong></p> 
     49 <p>Check out the new documents for designers at <strong><a
     50 href="{@docRoot}design/index.html">Android Design</a></strong>.</p>
     51 </div>
     52 
     53 
     54 <p>App widgets (sometimes just "widgets") are a feature introduced in Android 1.5 and vastly
     55 improved in Android 3.0 and 3.1. A widget can display an application's most timely or otherwise
     56 relevant information at a glance, on a user's Home screen. The standard Android system image
     57 includes several widgets, including a widget for the Analog Clock, Music, and other
     58 applications.</p>
     59 
     60 
     61 <img src="{@docRoot}images/widget_design/widget_examples.png"
     62   alt="Example app widgets in Android 4.0" id="widget_examples">
     63 
     64 <p class="img-caption"><strong>Figure 1.</strong> Example app widgets in Android 4.0.</p>
     65 
     66 
     67 <p>This document describes how to design a widget so that it fits graphically with other widgets and
     68 with the other elements of the Android Home screen such as launcher icons and shortcuts. It also
     69 describes some standards for widget artwork and some widget graphics tips and tricks.<p>
     70 
     71 <p>For information about developing widgets, see the <a
     72 href="{@docRoot}guide/topics/appwidgets/index.html">App Widgets</a> section of the <em>Developer's
     73 Guide</em>.</p>
     74 
     75 
     76 <h2 id="anatomy">Standard Widget Anatomy</h2>
     77 
     78 <p>Typical Android app widgets have three main components: A bounding box, a frame, and the widget's
     79 graphical controls and other elements. App widgets can contain a subset of the View widgets in
     80 Android; supported controls include text labels, buttons, and images. For a full list of available
     81 Views, see the <a href="{@docRoot}guide/topics/appwidgets/index.html#CreatingLayout">Creating the
     82 App Widget Layout</a> section in the <em>Developer's Guide</em>. Well-designed widgets leave some
     83 margins between the edges of the bounding box and the frame, and padding between the inner edges of
     84 the frame and the widget's controls.</p>
     85 
     86 
     87 <img src="{@docRoot}images/widget_design/widget_terms.png"
     88   alt="Widgets generally have margins and padding between bounding box, frame, and controls"
     89   id="widget_terms">
     90 
     91 <p class="img-caption"><strong>Figure 2.</strong> Widgets generally have margins between the
     92 bounding box and frame, and padding between the frame and widget controls.</p>
     93 
     94 
     95 <p class="note"><strong>Note: </strong> As of Android 4.0, app widgets are automatically given
     96 margins between the widget frame and the app widget's bounding box to provide better alignment with
     97 other widgets and icons on the user's home screen. To take advantage of this strongly recommended
     98 behavior, set your application's <a
     99 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html">targetSdkVersion</a> to 14 or
    100 greater.</p>
    101 
    102 <p>Widgets designed to fit visually with other widgets on the Home screen take cues from the other
    103 elements on the Home screen for alignment; they also use standard shading effects. All of these
    104 details are described in this document.</p>
    105 
    106 
    107 <h3 id="anatomy_determining_size">Determining a size for your widget</h3>
    108 
    109 <p>Each widget must define a <code>minWidth</code> and <code>minHeight</code>, indicating the
    110 minimum amount of space it should consume by default. When users add a widget to their Home screen,
    111 it will generally occupy more than the minimum width and height you specify. Android Home screens
    112 offer users a grid of available spaces into which they can place widgets and icons. This grid can
    113 vary by device; for example, many handsets offer a 4x4 grid, and tablets can offer a larger, 8x7
    114 grid. <strong>When your widget is added, it will be stretched to occupy the minimum number of cells,
    115 horizontally and vertically, required to satisfy its <code>minWidth</code> and
    116 <code>minHeight</code> constraints.</strong> As we discuss in <a href="#design">Designing Widget
    117 Layouts and Background Graphics</a> below, using nine-patch backgrounds and flexible layouts for app
    118 widgets will allow your widget to gracefully adapt to the device's Home screen grid and remain
    119 usable and aesthetically awesome.</p>
    120 
    121 <p>While the width and height of a cell&mdash;as well as the amount of automatic margins applied to
    122 widgets&mdash;may vary across devices, you can use the table below to roughly estimate your widget's
    123 minimum dimensions, given the desired number of occupied grid cells:</p>
    124 
    125 <table id="cellstable">
    126   <thead>
    127     <tr>
    128       <th># of Cells<br><small style="font-weight:normal">(Columns or Rows)</small></th>
    129       <th>Available Size (dp)<br><small style="font-weight:normal">(<code>minWidth</code> or
    130         <code>minHeight</code>)</small></th>
    131     </tr>
    132   </thead>
    133   <tbody>
    134     <tr>
    135       <td>1</td>
    136       <td>40dp</td>
    137     </tr>
    138     <tr>
    139       <td>2</td>
    140       <td>110dp</td>
    141     </tr>
    142     <tr>
    143       <td>3</td>
    144       <td>180dp</td>
    145     </tr>
    146     <tr>
    147       <td>4</td>
    148       <td>250dp</td>
    149     </tr>
    150     <tr>
    151       <td>&hellip;</td>
    152       <td>&hellip;</td>
    153     </tr>
    154     <tr>
    155       <td><em>n</em></td>
    156       <td>70 &times; <em>n</em> &minus; 30</td>
    157     </tr>
    158   </tbody>
    159 </table>
    160 
    161 <p>It is a good practice to be conservative with <code>minWidth</code> and <code>minHeight</code>,
    162 specifying the minimum size that renders the widget in a good default state. For an example of how
    163 to provide a <code>minWidth</code> and <code>minHeight</code>, suppose you have a music player
    164 widget that shows the currently playing song artist and title (vertically stacked), a
    165 <strong>Play</strong> button, and a <strong>Next</strong> button:</p>
    166 
    167 
    168 <img src="{@docRoot}images/widget_design/music_example.png"
    169   alt="An example music player widget" id="music_example">
    170 
    171 <p class="img-caption"><strong>Figure 3.</strong> An example music player widget.</p>
    172 
    173 
    174 <p>Your minimum height should be the height of your two TextViews for the artist and title, plus
    175 some text margins. Your minimum width should be the minimum usable widths of the
    176 <strong>Play</strong> and <strong>Next</strong> buttons, plus the minimum text width (say, the width
    177 of 10 characters), plus any horizontal text margins.</p>
    178 
    179 
    180 <img src="{@docRoot}images/widget_design/music_example_redline.png"
    181 alt="Example sizes and margins for minimum width/height calculations" id="music_example_redline">
    182 
    183 <p class="img-caption"><strong>Figure 4.</strong> Example sizes and margins for
    184 <code>minWidth</code>/<code>minHeight</code> calculations. We chose 144dp as an example good minimum
    185 width for the text labels.</p>
    186 
    187 
    188 <p>Example calculations are below:</p>
    189 
    190 <ul>
    191   <li><code>minWidth</code> = 144dp + (2 &times; 8dp) + (2 &times; 56dp) =
    192     <strong>272dp</strong></li>
    193   <li><code>minHeight</code> = 48dp + (2 &times; 4dp) = <strong>56dp</strong></li>
    194 </ul>
    195 
    196 <p>If there is any inherent content padding in your widget background nine-patch, you should add to
    197 <code>minWidth</code> and <code>minHeight</code> accordingly.</p>
    198 
    199 
    200 <h3 id="anatomy_resizable_widgets">Resizable widgets</h3>
    201 
    202 <p>Widgets can be resized horizontally and/or vertically as of Android 3.1, meaning that
    203 <code>minWidth</code> and <code>minHeight</code> effectively become the <em>default</em> size for
    204 the widget. You can specify the minimum widget size using <code>minResizeWidth</code> and
    205 <code>minResizeHeight</code>; these values should specify the size below which the widget would be
    206 illegible or otherwise unusable.</p>
    207 
    208 <p>This is generally a preferred feature for collection widgets such as those based on {@link
    209 android.widget.ListView} or {@link android.widget.GridView}.</p>
    210 
    211 
    212 <h3 id="anatomy_adding_margins">Adding margins to your app widget</h3>
    213 
    214 <p>As previously mentioned, Android 4.0 will automatically add small, standard margins to each edge
    215 of widgets on the Home screen, for applications that specify a <code>targetSdkVersion</code> of 14
    216 or greater. This helps to visually balance the Home screen, and thus <strong>we recommend that you
    217 do not add any extra margins outside of your app widget's background shape in Android
    218 4.0</strong>.</p>
    219 
    220 <p>It's easy to write a single layout that has custom margins applied for earlier versions of the
    221 platform, and has no extra margins for Android 4.0 and greater. See <a
    222 href="{@docRoot}guide/topics/appwidgets/index.html#AddingMargins">Adding Margins to App Widgets</a>
    223 in the <em>Developer's Guide</em> for information on how to achieve this with layout XML.</p>
    224 
    225 
    226 <h2 id="design">Designing Widget Layouts and Background Graphics</h2>
    227 
    228 <p>Most widgets will have a solid background rectangle or rounded rectangle shape. It is a best
    229 practice to define this shape using nine patches; one for each screen density (see <a
    230 href="{@docRoot}guide/practices/screens_support.html">Supporting Multiple Screens</a> for details).
    231 Nine-patches can be created with the <a
    232 href="{@docRoot}tools/help/draw9patch.html">draw9patch</a> tool, or simply with a
    233 graphics editing program such as Adobe&reg; Photoshop. This will allow the widget background shape
    234 to take up the entire available space. The nine-patch should be edge-to-edge with no transparent
    235 pixels providing extra margins, save for perhaps a few border pixels for <strong>subtle</strong>
    236 drop shadows or other subtle effects.</p>
    237 
    238 <p class="note"><strong>Note: </strong> Just like with controls in activities, you should ensure
    239 that interactive controls have distinct visual focused and pressed states using <a
    240 href="{@docRoot}guide/topics/resources/drawable-resource.html#StateList">state list
    241 drawables</a>.</p>
    242 
    243 
    244 <img src="{@docRoot}images/ninepatch_raw.png" alt="Nine-patch border pixels" id="ninepatch_raw">
    245 
    246 <p class="img-caption"><strong>Figure 5.</strong> Nine-patch border pixels indicating stretchable
    247 regions and content padding.</p>
    248 
    249 
    250 <p>Some app widgets, such as those using a {@link android.widget.StackView}, have a transparent
    251 background. For this case, each individual item in the StackView should use a nine-patch background
    252 that is edge-to-edge with little or no border transparent pixels for margins.</p>
    253 
    254 <p>For the contents of the widget, you should use flexible layouts such as {@link
    255 android.widget.RelativeLayout}, {@link android.widget.LinearLayout}, or {@link
    256 android.widget.FrameLayout}. Just as your activity layouts must adapt to different physical screen
    257 sizes, widget layouts must adapt to different Home screen grid cell sizes.</p>
    258 
    259 <p>Below is an example layout that a music widget showing text information and two buttons can use.
    260 It builds upon the previous discussion of adding margins depending on OS version. Note that the
    261 most robust and resilient way to add margins to the widget is to wrap the widget frame and contents
    262 in a padded {@link android.widget.FrameLayout}.</p>
    263 
    264 <pre>
    265 &lt;FrameLayout
    266   android:layout_width="match_parent"
    267   android:layout_height="match_parent"
    268   android:padding="@dimen/widget_margin"&gt;
    269 
    270   &lt;LinearLayout
    271     android:layout_width="match_parent"
    272     android:layout_height="match_parent"
    273     android:orientation="horizontal"
    274     android:background="@drawable/my_widget_background"&gt;
    275 
    276     &lt;TextView
    277       android:id="@+id/song_info"
    278       android:layout_width="0dp"
    279       android:layout_height="match_parent"
    280       android:layout_weight="1" /&gt;
    281 
    282     &lt;Button
    283       android:id="@+id/play_button"
    284       android:layout_width="@dimen/my_button_width"
    285       android:layout_height="match_parent" /&gt;
    286 
    287     &lt;Button
    288       android:id="@+id/skip_button"
    289       android:layout_width="@dimen/my_button_width"
    290       android:layout_height="match_parent" /&gt;
    291   &lt;/LinearLayout&gt;
    292 &lt;/FrameLayout&gt;
    293 </pre>
    294 
    295 <p>If you now take a look at the example music widget from the previous section, you can begin to
    296 use flexible layouts attributes like so:</p>
    297 
    298 
    299 <img src="{@docRoot}images/widget_design/music_example_layouts.png"
    300   alt="Excerpt flexible layouts and attributes for an example music widget"
    301   id="music_example_layouts">
    302 
    303 <p class="img-caption"><strong>Figure 6.</strong> Excerpt flexible layouts and attributes.</p>
    304 
    305 
    306 <p>When a user adds the widget to their home screen, on an example Android 4.0 device where each
    307 grid cell is 80dp &times; 100dp in size and 8dp of margins are automatically applied on all sizes,
    308 the widget will be stretched, like so:</p>
    309 
    310 
    311 <img src="{@docRoot}images/widget_design/music_example_stretched.png"
    312   alt="Music widget sitting on an example 80dp x 100dp grid with 8dp of automatic margins
    313   added by the system" id="music_example_stretched">
    314 
    315 <p class="img-caption"><strong>Figure 7.</strong> Music widget sitting on an example 80dp x 100dp
    316 grid with 8dp of automatic margins added by the system.</p>
    317 
    318 
    319 <h2 id="templates">Using the App Widget Templates Pack</h2>
    320 
    321 <p>When starting to design a new widget, or updating an existing widget, it's a good idea to first
    322 look at the widget design templates below. The downloadable package below includes nine-patch
    323 background graphics, XML, and source Adobe&reg; Photoshop files for multiple screen densities, OS
    324 version widget styles, and widget colors. The template package also contains graphics useful for
    325 making your entire widget or parts of your widget (e.g. buttons) interactive.</p>
    326 
    327 
    328 <img src="{@docRoot}images/widget_design/widget_template_excerpts.png"
    329   alt="Widget template excerpts" id="widget_template_excerpts">
    330 
    331 <p class="img-caption"><strong>Figure 8.</strong> Excerpts from the App Widget Templates Pack
    332 (medium-density, dark, Android 4.0/previous styles, default/focused/pressed states).</p>
    333 
    334 
    335 <p>You can obtain the latest App Widget Templates Pack archive using the link below:</p>
    336 
    337 <p style="margin-left:2em"><a href="{@docRoot}shareables/app_widget_templates-v4.0.zip">
    338   Download the App Widget Templates Pack for Android 4.0 &raquo;</a></p>
    339 
    340