1page.title=Android Studio Tips and Tricks 2@jd:body 3 4<div id="qv-wrapper"> 5<div id="qv"> 6 7 <h2>In this document</h2> 8 <ol> 9 <li><a href="#productivity-features">Productivity Shortcuts</a></li> 10 <li><a href="#intellij">Working with IntelliJ</a></li> 11 <li><a href="#key-commands">Key Commands</a></li> 12 </ol> 13 14 <h2>See also</h2> 15 <ol> 16 <li><a href="{@docRoot}sdk/index.html">Download Android Studio</a></li> 17 <li><a href="http://wiki.jetbrains.net/intellij/Android">IntelliJ IDEA Android Tutorials</a></li> 18 <li><a href="http://confluence.jetbrains.com/display/IntelliJIDEA/FAQ+on+Migrating+to+IntelliJ+IDEA">IntelliJ FAQ on migrating to IntelliJ IDEA</a></li> 19 </ol> 20 21</div> 22</div> 23 24<p>If you're unfamiliar with using Android Studio and the IntelliJ IDEA interface, this page 25provides some tips to help you get started with some of the most common tasks and productivity 26enhancements. </p> 27 28 29<h2 id="productivity-features">Productivity Shortcuts</h2> 30 31<p>Android Studio includes a number of features to help you be more productive in your coding. 32This section notes a few of the key features to help you work quickly and efficiently. 33</p> 34 35 36<h3>Smart Rendering</h3> 37<p>With smart rendering, Android Studio displays links for quick fixes to rendering errors. 38For example, if you add a button to the layout without specifying the <em>width</em> and 39<em>height</em> attributes, Android Studio displays the rendering message <em>Automatically 40add all missing attributes</em>. Clicking the message adds the missing attributes to the layout.</p> 41 42 43<h3> Bitmap rendering in the debugger</h3> 44<p>While debugging, you can now right-click on bitmap variables in your app and invoke 45<em>View Bitmap</em>. This fetches the associated data from the debugged process and renders 46the bitmap in the debugger. </p> 47<p><img src="{@docRoot}images/tools/studio-bitmap-rendering.png" style="width:350px"/></p> 48<p class="img-caption"><strong>Figure 1.</strong> Bitmap Rendering</p> 49 50 51<h3>Creating new files</h3> 52<p>You can quickly add new code and resource files by clicking the appropriate directory in the 53<strong>Project</strong> pane and pressing <code>ALT + INSERT</code> on Windows and Linux or 54<code>COMMAND + N</code> on Mac. Based on the type of directory selected, Android Studio 55offers to create the appropriate file type.</p> 56 57<p>For example, if you select a layout directory, press <code>ALT + INSERT</code> on Windows, 58and select <strong>Layout resource file</strong>, a dialog opens so you can name the file 59(you can exclude the {@code .xml} suffix) and choose a root view element. The editor then 60switches to the layout design editor so you can begin designing your layout.</p> 61 62 63<h3>Output window message filtering</h3> 64<p>When checking build results, you can filter messages by <em>message type</em> to quickly 65locate messages of interest.</p> 66<img src="{@docRoot}images/tools/studio-outputwindowmsgfiltering.png" style="width:200px"style="width:200px" /> 67<p class="img-caption"><strong>Figure 2.</strong> Filter Build Messages</p> 68 69 70<h3>Hierarchical parent setting</h3> 71<p>The activity parent can now be set in the Activity Wizard when creating a new 72activity. Setting a <em>hierarchal parent</em> sets the {@code Up} button to automatically 73appear in the app's Action bar when viewing a child activity, so the {@code Up} 74button no longer needs to be manually specified in the <em>menu.xml</em> file.</p> 75 76 77<h3>Creating layouts</h3> 78<p>Android Studio offers an advanced layout editor that allows you to drag-and-drop widgets 79into your layout and preview your layout while editing the XML.</p> 80 81<p>While editing in the <strong>Text</strong> view, you can preview the layout on devices by 82opening the <strong>Preview</strong> pane available on the right side of the window. Within the 83Preview pane, you can modify the preview by changing various options at the top of the pane, 84including the preview device, layout theme, platform version and more. To preview the layout on 85multiple devices simultaneously, select <strong>Preview All Screen Sizes</strong> from the 86device drop-down.</p> 87<p><img src="{@docRoot}images/tools/studio-previewall.png" style="width:350px"/></p> 88<p class="img-caption"><strong>Figure 3.</strong> Preview All Screens</p> 89 90<p>You can switch to the graphical editor by clicking <strong>Design</strong> at the 91bottom of the window. While editing in the Design view, you can show and hide the 92widgets available to drag-and-drop by clicking <strong>Palette</strong> on the left side of the 93window. Clicking <strong>Designer</strong> on the right side of the window reveals a panel 94with a layout hierarchy and a list of properties for each view in the layout.</p> 95 96 97<h3>Annotations</h3> 98<p>Android Studio provides coding assistance for using annotations from the 99{@link android.support.annotation Support-Annotations} library, part of the 100Support Repository. 101 102Adding a dependency for this library enables you to decorate your code with annotations to help 103catch bugs, such as null pointer exceptions and resource type conflicts. You can also create 104enumerated annotations to, for example, check that a passed parameter value matches a value from 105a defined set of constants. For more information, see 106<a href="{@docRoot}tools/debugging/annotations.html#annotations">Improving Code Inspection with 107Annotations</a>. 108</p> 109 110 111<h3>Java class decompiling</h3> 112<p>Android Studio allows you to look at what’s inside Java libraries when you don’t have access 113to the source code. </p> 114 115<p>The decompiler is built into Android Studio for easy access. To use this feature, right-click 116a class, method, or field from a library for which you do not have source file access and select 117<strong>decompile</strong>.</p> The decompiled source code appears. </p> 118 119<p>To adjust the Java decompiler settings, select 120<strong>File > Settings > Other Settings > Java Decompiler</strong>. </p> 121 122 123<h3>Debugging and performance enhancements</h3> 124<p>Android Studio offers debugging and performance enhancements such as:</p> 125<ul> 126 <li>Custom keymaps. To modify the current keymap, choose 127 <strong>File > Settings > Keymap</strong>. </li> 128 <li>Support for high density (Retina) displays on Windows and Linux. </li> 129 <li>Scratch files for quick prototyping without creating any project files. 130 <p>Choose <strong>Tools > New Scratch File</strong> to open a scratch file to quickly 131 build and run code prototypes. Together with Android Studio coding assistance, scratch 132 files allow you to quickly run and debug code updates with the support of all file operations. 133 By embedding code created with scripting languages, you can run your code from within the 134 scratch file.</p> 135 </li> 136</ul> 137 138 139<h3 id="live-template">Live templates</h3> 140<p>Live templates allow you to enter code snippets for fast insertion and completion of small chunks 141of code. To insert a live template, type the template abbreviations and press the 142Tab key. Android Studio inserts the code snippet associated with the template into 143your code. </p> 144 145<p>For example, entering the <code>newInstance</code> abbreviation followed by the 146Tab key inserts the code for a new fragment instance with argument placeholders. </p> 147 148<pre> 149public static $fragment$ newInstance($args$) { 150 $nullChecks$ 151 Bundle args = new Bundle(); 152 $addArgs$ 153 $fragment$ fragment = new $fragment$(); 154 fragment.setArguments(args); 155 return fragment; 156} 157</pre> 158 159<p>Similarly, the <code>fbc</code> abbreviation inserts a <code>findViewById</code> call along 160with cast and resource id syntax. </p> 161 162<pre> 163() findViewById(R.id.); 164</pre> 165 166<p>Use the <strong>File > Settings > Editor > Live Templates</strong> menu option to 167display the full list of supported live templates and customize the inserted code. </p> 168 169 170 171<h2 id="intellij">Working with IntelliJ-based Coding Practices</h3> 172 173<p>This section list just a few of the code editing 174practices you should consider using when creating Android Studio apps. </p> 175 176<p>For complete user documentation for the IntelliJ IDEA interface (upon which Android Studio 177is based), refer to the 178<a href="http://www.jetbrains.com/idea/documentation/index.jsp">IntelliJ IDEA documentation</a>.</p> 179 180 181<h3><em>Alt + Enter</em> key binding</h3> 182<p>For quick fixes to coding errors, the IntelliJ powered IDE implements the <em>Alt + Enter</em> 183key binding to fix errors (missing imports, variable assignments, missing references, etc) when 184possible, and if not, suggest the most probable solution. </p> 185 186 187<h3><em>Ctrl + D</em> key binding</h3> 188<p>The <em>Ctrl + D</em> key binding is great for quickly duplicating code lines or fragments. 189Simply select the desired line or fragment and enter this key binding. </p> 190 191 192<h3>Navigate menu</h3> 193<p>In case you're not familiar with an API class, file or symbol, the <em>Navigate</em> menu lets 194you jump directly to the class of a method or field name without having to search through 195individual classes. </p> 196 197 198<h3>Inspection scopes</h3> 199<p>Scopes set the color of code segments for easy code identification and location. For example, 200you can set a scope to identify all code related to a specific action bar. </p> 201 202 203<h3>Injecting languages</h3> 204<p>With language injection, the Android Studio IDE allows you to work with islands of different 205languages embedded in the source code. This extends the syntax, error highlighting and coding 206assistance to the embedded language. This can be especially useful for checking regular expression 207values inline, and validating XML and SQL statements.</p> 208 209 210<h3>Code folding</h3> 211<p>This allows you to selectively hide and display sections of the code for readability. For 212example, resource expressions or code for a nested class can be folded or hidden in to one line 213to make the outer class structure easier to read. The inner class can be later expanded for 214updates. </p> 215 216 217<h3>Image and color preview</h3> 218<p>When referencing images and icons in your code, a preview of the image or icon appears 219(in actual size at different densities) in the code margin to help you verify the image or icon 220reference. Pressing {@code F1} with the preview image or icon selected displays resource asset 221details, such as the <em>dp</em> settings. </p> 222 223 224<h3>Quick F1 documentation</h3> 225<p>You can now inspect theme attributes using <strong>View > Quick Documentation</strong> 226(<strong>F1</strong>), 227see the theme inheritance hierarchy, and resolve values for the various attributes.</p> 228 229<p>If you invoke <strong> View > Quick Documentation</strong> (usually bound to F1) on the theme 230attribute <em>?android:textAppearanceLarge</em>, you will see the theme inheritance hierarchy and 231resolved values for the various attributes that are pulled in.</p> 232 233 234 235<h3 id="key-commands">Keyboard Commands</h3> 236 237<p>The following tables list keyboard shortcuts for common operations.</p> 238 239<p class="note"><strong>Note:</strong> This section lists Android Studio keyboard shortcuts 240for the default keymap. To change the default keymap on Windows and Linux, go to 241<strong>File</strong> > <strong>Settings</strong> > <strong>Keymap</strong>. If you're 242using Mac OS X, update your keymap to use the Mac OS X 10.5+ version keymaps under 243<strong>Android Studio > Preferences > Keymap</strong>.</p> 244 245 246<p class="table-caption"><strong>Table 1.</strong> Programming key commands</p> 247<table> 248<tr><th>Action</th><th>Android Studio Key Command</th></tr> 249 250<tr> 251 <td>Command look-up (autocomplete command name)</td> 252 <td>CTRL + SHIFT + A</td> 253</tr> 254 255<tr> 256 <td>Project quick fix</td> 257 <td>ALT + ENTER</td> 258</tr> 259 260<tr> 261 <td>Reformat code</td> 262 <td>CTRL + ALT + L (Win)<br> 263 OPTION + CMD + L (Mac)</td> 264</tr> 265 266<tr> 267 <td>Show docs for selected API</td> 268 <td>CTRL + Q (Win)<br> 269 F1 (Mac)</td> 270</tr> 271 272<tr> 273 <td>Show parameters for selected method</td> 274 <td>CTRL + P</td> 275</tr> 276 277<tr> 278 <td>Generate method</td> 279 <td>ALT + Insert (Win)<br> 280 CMD + N (Mac)</td> 281</tr> 282 283<tr> 284 <td>Jump to source</td> 285 <td>F4 (Win)<br> 286 CMD + down-arrow (Mac)</td> 287</tr> 288 289<tr> 290 <td>Delete line</td> 291 <td>CTRL + Y (Win)<br> 292 CMD + Backspace (Mac)</td> 293</tr> 294 295<tr> 296 <td>Search by symbol name</td> 297 <td>CTRL + ALT + SHIFT + N (Win)<br> 298 OPTION + CMD + O (Mac)</td> 299</tr> 300 301</table> 302 303 304 305 306<p class="table-caption"><strong>Table 2.</strong> Project and editor key commands</p> 307<table> 308<tr><th>Action</th><th>Android Studio Key Command</th></tr> 309 310<tr> 311 <td>Build</td> 312 <td>CTRL + F9 (Win)<br> 313 CMD + F9 (Mac)</td> 314</tr> 315 316<tr> 317 <td>Build and run</td> 318 <td>SHIFT + F10 (Win)<br> 319 CTRL + R (Mac)</td> 320</tr> 321 322<tr> 323 <td>Toggle project visibility</td> 324 <td>ALT + 1 (Win)<br> 325 CMD + 1 (Mac)</td> 326</tr> 327 328<tr> 329 <td>Navigate open tabs</td> 330 <td>ALT + left-arrow; ALT + right-arrow (Win)<br> 331 CTRL + left-arrow; CTRL + right-arrow (Mac)</td> 332</tr> 333 334</table> 335 336<p>For a complete keymap reference guide, see the 337<a href="http://www.jetbrains.com/idea/documentation/index.jsp">IntelliJ IDEA</a> 338documentation.</p> 339