1page.title=Using Code Templates 2page.image=images/cards/card-using-code-templates_16x9_2x.png 3page.metaDescription=Quickly create Android app projects with various UI or functional components. 4page.tags=studio,templates,firstapp 5@jd:body 6 7 <div id="qv-wrapper"> 8 <div id="qv"> 9 <h2>In this document</h2> 10 11 <ol> 12 <li><a href="#app-templates">Application Templates</a> 13 <ol> 14 <li><a href="#blank-activity">BlankActivity Template</a></li> 15 <li><a href="#full-screen-activity">Full Screen Activity Template</a></li> 16 <li><a href="#master-detail-activity">Master Detail Flow Template</a></li> 17 </ol> 18 </li> 19 20 <li><a href="#activity-templates">Activity Templates</a> 21 <ol> 22 <li><a href="#login-activity">Login Activity Template</a></li> 23 <li><a href="#settings-activity">Settings Activity Template</a></li> 24 </ol> 25 </li> 26 <li><a href="#object-templates">Other Templates</a></li> 27 </ol> 28 29 </div> 30 </div> 31 32<img style="float:right" src="{@docRoot}images/cards/card-using-code-templates_16x9_2x.png"> 33<p>The SDK tools provide templates for quickly creating Android application projects with the basic 34 structure or for adding components to your existing application modules. The code templates 35 provided by the Android SDK follow the Android design and development guidelines to get you on the 36 right track to creating a beautiful, functional application.</p> 37 38<p>There are several types of Android code templates, which can create anything from an entire 39 application down to specific application components. The main categories of code templates are as 40 follows:</p> 41 42<ul> 43 <li><a href="#app-templates">Application Templates</a></li> 44 <li><a href="#activity-templates">Activity Templates</a></li> 45 <li><a href="#object-templates">Other Templates</a></li> 46</ul> 47 48 49<h2 id="app-templates">Application Templates</h2> 50 51<p>Application templates create basic Android application modules that you can immediately run and test 52 on your Android device. These templates are available when you create a new Android module, 53 though you can also use these templates to <a href="#activity-templates">add new activities</a> 54 to an existing module.</p> 55 56<p>To use Android application templates:</p> 57 58<ol> 59 <li>In Android Studio, select <strong>File > New > Module</strong>.</li> 60 <li>Enter the settings for your application, including <strong>Application Name</strong>, 61 <strong>Company Domain</strong>, <strong>Package name</strong>, and <strong>minimum SDK</strong>, 62 then click <strong>Next</strong>.</li> 63 <li>Select an application template to use, then click <strong>Next</strong>. For example, 64 <ul> 65 <li><a href="#blank-activity">BlankActivity</a></li> 66 <li><a href="#full-screen-activity">FullScreenActivity</a></li> 67 <li><a href="#master-detail-activity">MasterDetailFlow</a></li> 68 69 </ul> 70 </li> 71 <li>Enter the settings for your activity, including <strong>Activity Name</strong>, 72 <strong>layout Name</strong>, <strong>Title</strong>, and <strong>Menu Resource Name</strong>, 73 then click <strong>Finish</strong>.</li> 74</ol> 75 76<p class="note"> 77 <strong>Note:</strong> The other activity template options also create applications, however these 78 applications require further modification before they can be launched on an Android device. 79</p> 80 81 82<h3 id="blank-activity">Blank Activity Template</h3> 83 84<table> 85 <tr> 86 <th width="206px">Example</th> 87 88 <th>Description</th> 89 </tr> 90 91 <tr> 92 <td><img src="{@docRoot}images/code_templates/ba-no-navigation.png" alt="" /> 93 </td> 94 95 <td><p>The <strong>BlankActivity</strong> template with the <strong>Navigation Type: 96 None</strong> option creates a simple application that follows the 97 <a href="{@docRoot}design/index.html">Android Design</a> guidelines. Use this template to 98 create a basic, minimal app as a starting point for your project.</p> 99 100 <p>This template includes:</p> 101 102 <ul> 103 <li>Title bar ({@link android.app.ActionBar} on Android 3.0 and later)</li> 104 <li>Options menu (action overflow on Android 3.0 and later) </li> 105 <li>Basic layout</li> 106 </ul> 107 </td> 108 </tr> 109 110 <tr> 111 <td><img src="{@docRoot}images/code_templates/ba-tabs.png" alt="" /> 112 </td> 113 114 <td><p>The <strong>BlankActivity</strong> template with the <strong>Navigation Type: 115 Tabs</strong> or <strong>Tabs + Swipe</strong> option creates an application with 116 three sections based on the {@link android.app.Fragment} class and a tabbed user 117 interface.</p> 118 119 <p>This template includes:</p> 120 121 <ul> 122 <li>{@link android.app.ActionBar} for tab controls</li> 123 <li>{@link android.app.Fragment} objects for section content</li> 124 <li>Optional swipe gesture support based on the 125 <a href="{@docRoot}design/patterns/swipe-views.html">swipe view</a> design pattern, 126 which extends {@link android.support.v4.app.FragmentPagerAdapter} to manage section 127 fragments</li> 128 </ul> 129 </td> 130 </tr> 131 132 <tr> 133 <td><img src="{@docRoot}images/code_templates/ba-title-strip.png" alt="" /> 134 </td> 135 136 <td><p>The <strong>BlankActivity</strong> template with the <strong>Navigation Type: 137 Swipe Views + Title Strip</strong> option creates an application with three 138 {@link android.app.Fragment} sections, a compact title strip header (known as 139 <a href="{@docRoot}design/building-blocks/tabs.html#scrollable">Scrollable Tabs</a> in the 140 <a href="{@docRoot}design/index.html">Android Design</a> guide) and swipe navigation between 141 the sections, based on the <a href="{@docRoot}design/patterns/swipe-views.html">swipe 142 view</a> design pattern.</p> 143 144 <p>This template includes:</p> 145 146 <ul> 147 <li>{@link android.support.v4.view.PagerTitleStrip} for section titles</li> 148 <li>{@link android.app.Fragment} objects for section content</li> 149 <li>{@link android.support.v4.app.FragmentPagerAdapter} to manage section fragments</li> 150 </ul> 151 </td> 152 </tr> 153 154 <tr> 155 <td><img src="{@docRoot}images/code_templates/ba-dropdown.png" alt="" /> 156 </td> 157 158 <td><p>The <strong>BlankActivity</strong> template with the <strong>Navigation Type: 159 Dropdown</strong> option creates an application that extends 160 {@link android.support.v4.app.FragmentActivity}, containing three 161 {@link android.app.Fragment} sections, with an {@link android.app.ActionBar} using list mode 162 navigation.</p> 163 164 <p>This template includes:</p> 165 166 <ul> 167 <li>{@link android.app.ActionBar} for list mode navigation</li> 168 <li>{@link android.app.Fragment} objects for section content</li> 169 </ul> 170 </td> 171 </tr> 172</table> 173 174 175<h3 id="full-screen-activity">Full Screen Activity Template</h3> 176 177<table> 178 <tr> 179 <th width="240px">Example</th> 180 181 <th>Description</th> 182 </tr> 183 184 <tr> 185 <td><img src="{@docRoot}images/code_templates/full-screen-activity.png" alt="" /> 186 </td> 187 188 <td><p>This template provides an implementation of an activity which alternates between a 189 primary, full screen view and a view with standard user interface controls, including the 190 notification bar and application title bar. The full screen view is the default and a user 191 can activate the standard view by touching the device screen.</p> 192 193 <p>This template includes:</p> 194 195 <ul> 196 <li>{@code SystemUiHider} implementation that manages hiding of the system user interface 197 using a version-compatible approach</li> 198 <li>Basic layout</li> 199 </ul> 200 </td> 201 </tr> 202</table> 203 204 205<h3 id="master-detail-activity">Master Detail Flow Template</h3> 206 207<table> 208 <tr> 209 <th width="350px">Example</th> 210 211 <th>Description</th> 212 </tr> 213 214 <tr> 215 <td><img src="{@docRoot}images/code_templates/master-detail-flow.png" alt=""/> 216 </td> 217 218 <td><p>This template creates an adaptive layout for a set of items and associated details. On a 219 tablet device, the item list and item details are displayed on the same screen. On a smaller 220 device, the list and details are displayed on separate screens.</p> 221 222 <p class="note"> 223 <strong>Note:</strong> This template follows the recommendations of the 224 <a href="{@docRoot}training/multiscreen/index.html">Designing for Multiple Screens</a> 225 Android training. 226 </p> 227 228 <p>This template includes:</p> 229 230 <ul> 231 <li>Adaptive layout using 232 <a href="{@docRoot}guide/topics/resources/providing-resources.html#AlternativeResources" 233 >alternative resource</a> XML files</li> 234 <li>{@link android.support.v4.app.FragmentActivity}, {@link android.app.Fragment} and 235 {@link android.support.v4.app.ListFragment} implementations</li> 236 </ul></td> 237 </tr> 238</table> 239 240 241<h2 id="activity-templates">Activity Templates</h2> 242 243<p>Android activity templates provide options to add new activities to your existing 244 application.</p> 245 246<p>To use Android activity templates:</p> 247 248<ol> 249 <li>Right click the project folder of the Android application where you want to add an 250 activity.</li> 251 <li>Select <strong>New > Other...</strong></li> 252 <li>Select <strong>Android > Android Activity</strong>, then click <strong>Next</strong>.</li> 253 <li>Select an activity template, then follow the instructions to add it to your existing 254 application. 255 <ul> 256 <li><a href="#login-activity">LoginActivity</a></li> 257 <li><a href="#settings-activity">SettingsActivity</a></li> 258 <li><a href="#blank-activity">BlankActivity</a></li> 259 <li><a href="#full-screen-activity">FullScreenActivity</a></li> 260 <li><a href="#master-detail-activity">MasterDetailFlow</a></li> 261 </ul> 262 </li> 263</ol> 264 265<p>These templates create the same type of activity as they do when used as an application template, 266however the following templates create activities which are specifically intended to be used as part 267of an existing application.</p> 268 269 270<h3 id="login-activity">Login Activity Template</h3> 271 272<table> 273 <tr> 274 <th width="206px">Example</th> 275 276 <th>Description</th> 277 </tr> 278 279 <tr> 280 <td><img src="{@docRoot}images/code_templates/login-activity.png" alt="" /> 281 </td> 282 283 <td><p>This activity template provides input fields and a sample implementation of 284 an {@link android.os.AsyncTask} that asks users to login or register with their credentials.</p> 285 286 <p>This template includes:</p> 287 288 <ul> 289 <li>Recommended user interface for requesting login information</li> 290 <li>{@link android.os.AsyncTask} implementation for handing network operations separately 291 from the main user interface thread</li> 292 <li>Progress indicator during network operations</li> 293 </ul> 294 </td> 295 </tr> 296</table> 297 298 299<h3 id="settings-activity">Settings Activity Template</h3> 300 301<table> 302 <tr> 303 <th width="206px">Example</th> 304 305 <th>Description</th> 306 </tr> 307 308 <tr> 309 <td><img src="{@docRoot}images/code_templates/settings-activity.png" alt="" /> 310 </td> 311 312 <td><p>This template extends the {@link android.preference.PreferenceActivity} class and uses an 313 XML file to create preference settings. This template also demonstrates how to implement 314 several data types for settings.</p> 315 316 <p>This template includes:</p> 317 318 <ul> 319 <li>Activity extending {@link android.preference.PreferenceActivity}</li> 320 <li>Preference values defined using XML files added to the {@code res/xml/} directory of 321 your project.</li> 322 </ul> 323 </td> 324 </tr> 325</table> 326 327 328<h2 id="object-templates">Other Templates</h2> 329 330<p>Android object templates provide options to add new components to your existing application, 331including the previously mentioned activities as well as the following additional items:</p> 332 333<p>To use Android object templates:</p> 334 335<ol> 336 <li>Right-click the module folder of the Android application where you want to add a code 337 component.</li> 338 <li>Select <strong>New</strong></li> 339 <li>Select the object type and template, then follow the instructions to add it to your existing 340 application. 341 <ul> 342 <li>AIDL</li> 343 <li>Activity</li> 344 <li>Folder</li> 345 <li>Fragment</li> 346 <li>Google</li> 347 <li>Other</li> 348 <li>Service</li> 349 <li>UI Component</li> 350 <li>Wear</li> 351 <li>Widget</li> 352 <li>XML</li> 353 </ul> 354 </li> 355</ol> 356