Android Studio Shortcuts

As Android developers, a lot of our time is spent staring at Gradle builds. While the best remedy to speed up your builds is to invest in a powerful machine, you can also save time by using shortcuts efficiently.

There are plenty of shortcuts available in the IDE — I’ll assume you’re aware of the most commonly used, basic ones (specifically for macOS) such as:

  • Shift + Cmd + O: Go to a file
  • Cmd + F9: Build project
  • Cmd + E : Recent file
  • Cmd + Space : Basic code completion
  • Cmd + Option + L : Reformat the selected code
  • Option + F6 : Find usages
  • Shift + F6 : Refactor/rename
  • Cmd + Alt + /: Comment uncomment a block of code

In the next few sections, we’ll walk through some essential Android studio shortcuts that you might not know.

Using a mouse or a trackpad frequently breaks the workflow and hampers productivity. Let’s start building a good habit — using the keyboard more often.

I hope these tips will speed up your workflow.

Goto Next Or Previously Edited Code

We can easily lose track of the last edited changes from our codebase. By using the Cmd + square brackets [] you can easily switch your cursor to the last and next edited positions in the code.

Shortcut: Cmd + [ and Cmd + ]

Command + Shift + Backspace lets you navigate through the cursor history in your codebase.

Show List Of Function Parameters

When invoking a function, especially ones with more than a few arguments, it’s handy to know the list of parameters available, their order, and types. Knowing this helps minimize the chances of passing wrong arguments.

The following shortcut highlights the function’s signature with the current parameter in bold.

Shortcut: Cmd + P

Or, in Windows, (Ctrl + P):

Select Current Code Block

To select the current code block to start or the end we can simply use the following shortcut.

Shortcut: Option+Command+Shift + { or + }

Sometimes, finding the parentheses pair becomes tricky — in nested code blocks, for example. To figure the correct opening or closing pair, without spending too long staring at the highlighted brackets, we simply do a Option + Command + ] or + [.

Expand or Collapse all Methods

The codebases of android applications are often interspersed with callback hells and anonymous classes(OnClickListeners and more). Navigating through such codebases can be frustrating. Luckily, Android studio has a couple of shortcuts to expand and collapse all methods.

All Methods Expand/Collapse: Cmd + Shift + +/-

For Windows users: CTRL + SHFT And +/-.

Additionally, there’s a shortcut to selectively collapse/expand the current block as well: Command + +/-.

Quickly Replace Method Name

We often come across scenarios where there are several functions with the same arguments and similar names. What we want to do is to change the method name. Typically, we’d put the cursor in the middle of a function name and look for the alternative names. Upon finding one, the IDE adds the new name but doesn’t remove the previous one. This is annoying as you have to manually remove the previous name.

However, there’s a less well-known shortcut that can quickly replace the name. Instead of pressing “Enter” to auto-fill the new name, press “Tab”.

Shortcut: Use “Tab” instead of “return” to replace a name

Efficiently Search Your Codebases

There are many options to search for classes, functions, and interfaces in your codebase.

  • Cmd + F searches with the current file.
  • Shift + Cmd + F lets you search in the path.
  • Cmd + Option + O allows you to directly search by symbol name.

Best of all is the Shift + Shift shortcut command that lets you search everywhere in your codebase. Also, to quickly search a file in your codebase, especially when there are plenty of with the same prefixes, you can simply type the first letters of the class name words. For example, in order to look for a file named ActivityTaxReceipt2 you can quickly type ATR2.

Find actions

The search finder also lets you quickly look for actions — actions you might use occasionally, but for which you can’t recall the shortcut at the moment. It uses a powerful fuzzy search to retrieve the shortcut based on what you enter in natural language.

Shortcut: Cmd + Shift + A

Use the Tab to switch between actions and search all codebase.

Code Generation

Data classes in Kotlin reduce a lot of boilerplate code but typing out all the toString, Parcelable implementations and getter-setter methods of other classes can still eat up a lot of time. Luckily, we can leverage the following code generation shortcuts:

  • Cmd + N lists out all the options available, such as override methods and implement interfaces, available in the editor pane.
  • Command+O presents you with the list of methods you can override.

Enclose statements

To quickly surround a bunch of statements in a if-else ,try-catch, or loops, etc, use the following shortcuts:

Mac: Cmd + Option + T
Windows: Ctrl + Alt + T

Live templates: Cmd + J

Despite our best efforts, we spend a lot of time writing redundant code. For instance, defining iteration loops, Toasts, initializing views (findViewById) and printing log statements.

Luckily, Android studio provides a lot of built-in code snippets that are available in code completion using the Cmd + J shortcut. Once selected, you can simply tab through to specify the required arguments.

You can view the full list of Live templates and even create a custom one from Preferences > Editor > Live Templates.

Goto Declaration

When you’re new to a codebase, quickly navigating to the declaration of a variable, method or class is probably the most common thing you do as part of a code reading.

Doing a Cmd + Click quickly takes us to the declaration. The keyboard shortcuts offer even more flexibility.

Cmd + B displays a list of a hierarchy of all the places where the class, variable or method is called from the current code. To just look for the constructor calls of a class, invoke the above shortcut on the constructor, as shown below:

There’s another variant that returns the list of implementations of the class:Option + Command + B.

Fixing Errors and Optimising Imports

Let’s start with the most common use case. When the cursor is on the line of an error, more often than not, we hover the mouse to show a detailed description of the red-underlined code. Not only does the IDE have a lag when displaying the pop-up box, but it also slows down our workflow. Android Studio provides the following shortcuts.

Hover the underlined error: Cmd + F1

Go to the next highlighted errors

A less well known Android Studio shortcut takes you to the next error that’s highlighted by the IDE in the currently opened file (or warning if there are no errors). Simply press F2.

Auto-fix an error

By using the shortcut Option + Enter on an error, Android studio automatically fixes it using the recommended error, if available.

Optimizing imports

To remove the unused imports and sort the statements by group, the following shortcut is used: Ctrl + Option + O

Note: Android Studio doesn’t do the best job when auto-importing packages — especially with the ambiguous ones. So we can explicitly fix that from Preferences > Editor > Auto Import by:

  • Changing “Insert imports on paste” dropdown value to “All.”
  • Enabling the Add unambiguous imports on the fly” option.

Clipboard History

Another little known feature of Android Studio is the much-needed clipboard history. The IDE stores a list of contents you have copied in the recent past. You can then choose to paste one or more of them at the same time.

Shortcut: Cmd + Shift + V

Modifying Methods

Android Studio provides shortcuts that let you modify the signature of a function and extract a block of code into a new method.

Changing a method signature

Mac: Cmd + f6
Windows: Control + f6

Extracting methods

Mac: Cmd + Option + M
Windows: Ctrl + Alt + M

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raviraj Desai

Raviraj Desai

Senior Manager at Axis Bank (Android — Kotlin lover. Tech stack: MVVM, Dagger2, Coroutines, RxJava2, Android Architecture Component ,Flutter Enthusiast ,Ejabber