Android Studio Tips and Tricks — Part 1

We as developers always strive to be more productive while writing code but there are always some things which get in the way of achieving it. One such thing might be the over-dependence on mouse while writing code. Constantly switching between your keyboard and mouse interrupts your flow and can cause you to lose concentration which causes waste of time and breaks the overall flow. One way to avoid this would be to completely refrain from using mouse while writing code or you can go one step further and unplug your mouse altogether :)

The goal of this multi-part series is to get you familiar with some of the tips and tricks in Android Studio that can help you decrease your dependence on mouse and get productive. Most of these tips and tricks are available as part of IntelliJ on which Android Studio is built.

In the first part of the series we are going to learn some keyboard shortcuts for refactoring/editing our code. So let’s get started.

Note:

  1. All the keyboard shortcuts shown in this series are for Windows/Linux.
  2. The keymap used in Android Studio on my machine is Default for XWin.
  3. The little green popup showing keyboard shortcuts at the bottom is a nice little plugin called Presentation Assistant.
  • Copy line: To copy a whole line place your cursor anywhere on that line and press Ctrl+C. This will automatically select the whole line and copy it to the clipboard.
  • Cut line: Place your cursor anywhere on the line and press Ctrl+X. This will cut the whole line and place it on the clipboard.
  • Show clipboard: Sometimes you wish to paste a previously copied text but that text is already overwritten by some other text that you recently copied. What would you do in this case? Find that text again, copy it and then paste it? Sounds exhausting. Pressing Ctrl+Shift+V will open up the clipboard which will show all the previously copied texts and you can select the one that you want to paste. Too easy, right?
  • Duplicate line: If you wish to duplicate a line in the code then place the cursor anywhere on that line and press Ctrl+D. This will create the duplicate line directly below the selected line as shown:
  • Check parameter information: If the argument list of the method call is long, it can become confusing about the order of the arguments and type of those arguments. Pressing Ctrl+P will give you all the information(type, order) about the arguments that should be passed to that method call in a popup. So the information popup stays on the window while you fill in the required arguments in to the method call.
  • Extend/Shrink selection: You can select code in Android Studio by holding down the Ctrl key and keep pressing key W which will extend selection and for shrinking the selection, hold down Shift+Ctrl and keep pressing key W until you select the desired code.
  • Access Terminal: You can access terminal from within Android Studio. Press Alt+F12 in Android Studio and it will open a terminal window in your project’s path. If you like to use your VCS like Git from terminal like me or you want to run gradle tasks from a terminal, then this can surely help you.

Refactoring Code

If you ever want to refactor your code, never do it manually because that will most probably mess things up and you will end up wasting a lot of your precious time. Android Studio have a lot of powerful features that can refactor your code for you.

  • Extract variable(Ctrl+Alt+V): You can introduce a variable for your object using this shortcut if one is not defined in the method. Android Studio is smart enough to find all the occurrences of the same object in the method and will offer you a choice to replace only the single occurrence or all of them with the new variable.
  • Extract method(Ctrl+Alt+M): There are scenarios where a method is starting to get unnecessarily big and you may want to shrink it by taking some of the functionality from it and extracting that functionality to a separate method or you may have same piece of code at multiple places and you want put that piece of code into its own method. In situations like these select the lines that you wish to extract into a new method and press Ctrl+Alt+M. Android Studio will ask you for the method name and create the method for you. If those extracted lines of code depend on some local variable in the original method, then Android Studio will automatically set those variables as parameter in the new method. Pretty cool right!
  • Extract constant(Ctrl+Alt+C): This will make a local variable into a constant i.e. public static final. Place the cursor at the variable name and press Ctrl+Alt+C and it will automatically extract the variable or expression into a constant.
  • Extract field(Ctrl+Alt+F): This will make a local variable into a instance variable. Place the cursor anywhere on the variable declaration and press Ctrl+Alt+F and it will extract the variable into class member.
  • Extract parameter(Ctrl+Alt+P): This will make a local variable into a method parameter. Place the cursor on the variable and press Ctrl+Alt+P and it will set the variable as one of the arguments to the method.
  • Rename(Shift + F6): If you ever want to rename anything in the Android Studio, be it a file name or method name or variable name, make sure that you only use this shortcut to do it. It will find all the occurrences of the thing that you want to rename and will automatically rename them also, saving you the time for doing all that manually.
  • Move code(Ctrl+Shift+up/down arrow key): Select the method or the piece of code that you wish to move and hold down keys Ctrl+Shift and keep pressing up/down key until you get your code exactly where you wanted it.
  • Surround code(Ctrl+Alt+T): You can surround a piece of code in your method with various surround templates available in Android Studio for e.g. if block, try/catch block, for loop, etc. Select the piece of code that you wish to surround and press Ctrl+Alt+T. Android Studio will show a popup menu with list of surround templates available and you can choose whatever fits your needs.

Tip: Use Key Promoter Plugin for Android Studio

Further Reference

  1. Mouseless Driven Development by Hadi Hariri
  2. The powerful Android Studio by Saulmm
  3. (About) 10 Things You (Probably) Didn’t Know You Could do in Android Studio by Reto Meier
  4. Fragmented Podcast Episode 48
  5. Android Studio Productivity Tips by Donn Felker

That’s it for this part. Thanks for reading, and stay tuned for the next part, where I discuss mouseless navigation between code/files.