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