Android Studio Tips and Tricks

Master some hidden gems of Android Studio and improve your overall development efficiency with these Android Development tips and tricks. By Nishant Srivastava.

Leave a rating/review
Download materials
Save for later
Share

Android app development has changed a lot since Android debuted. To be successful today, you need to know how to use an important tool of the trade: Android Studio.

Android Studio is the official Integrated Development Environment (IDE) for Google’s Android operating system. It is built on JetBrains’ IntelliJ IDEA software and created for Android development.

While you can write your code by hand, if you master the IDE, you’ll find you’ll use it on a daily basis to get the most out of Android Development and improve your efficiency and productivity.

Note: An Integrated Development Environment (IDE) is an app that provides comprehensive facilities to computer programmers for software development. An IDE consists of at least a source code editor, build automation tools and a debugger. Most modern IDEs also have intelligent code completion.

In this tutorial, you’ll master navigating the codebase, refactoring, debugging and using the right tool/plugin at the right time to become a beast of Android Studio!

So fire up your IDE and walk through this goldmine of productivity tips and tricks. :]

Getting Started

Since it’s based on IntelliJ IDEA, Android Studio inherits a lot of keyboard shortcuts. Additionally, many of its panels also have their own quick keyboard shortcuts.

Instead of juggling between a mouse or trackpad and a keyboard, these quick shortcuts let you keep your fingers on the keyboard and control everything smoothly.

In the next section, you’ll learn some of the most common keyboard shortcuts.

Note: Inside Android Studio, the set keyboard shortcuts correspond to the default MacOSX key mapping. For Windows and Linux, you’ll find the shortcuts to use either in the animated gifs or written out in the tutorial, where necessary.

To get started with this tutorial, click the Download Materials button at the top or bottom of the page to download the starter project.

If you already have Android Studio 3.4.1 or later open, click FileImport Project and select the top-level project folder you downloaded.

If not, fire up Android Studio and select Open an existing Android Studio project from the Welcome screen, then choose the top-level project folder for the starter project you downloaded.

Code Refactoring

Your spontaneous reaction to refactoring might be that it’s too much work and leads to unintended bugs in the codebase. If your only option was to refactor your code by hand using a simple text editor, you’d be right. However, if you offload all the steps involved to the IDE and let it execute the refactoring task as an automated process, it becomes a piece of cake. :]

In this section, you’ll explore some of the Android Studio features which simplify the refactoring overhead to a few keystrokes, letting you refactor with confidence!

Using Shortcuts to Move Code Around

Moving code around is one of the most frequents tasks in a developer’s day-to-day activities. When you introduce new changes, you’ll often have to shift a line of code. Using Android Studio, you have a variety of shortcuts to help you do this without introducing errors.

Shifting Code Up and Down

You can easily push code up or pull it down by using:

Move code up and down width=

Move methods up and down

  1. For lines of code:
    • On MacOSX: + + and + +
    • On Windows/Linux: Shift + Alt + and Shift + Alt +

    Move code up and down width=

  2. For methods:
    • On MacOSX: + + and + +
    • On Windows/Linux: Shift + Ctrl + and Shift + Ctrl +

    Move methods up and down

Moving Sections of Code

If you’d like move a section of code to its own class or to another package, use the F6 key, as shown below:

Note: This works for all top-level declarations and for Java’s static methods and constants.

Duplicating Code

As you are coding, you might find that you want to duplicate a section or a line of code. To duplicate code quickly, use + D on MacOSX and Ctrl + D on Windows/Linux:

Deleting Code

Oh, no, you’ve made a mistake! How can you quickly delete code? The shortcut + Backspace on MacOSX and Ctrl + Y on Windows/Linux comes to the rescue:

Note: These shortcuts work regardless of the cursor’s position on the line.

Renaming Code Constructs

As you code, you might find that you need to rename your constructs. Manual renaming is out of the picture: It’s too much work and it’s prone to human error. Plus, switching to your mouse takes too much time.

To rename anything, use the key combination of + F6. Here’s how it’s done:

  1. Renaming a variable/field:
    Rename variable
  2. Renaming a method:
    Rename method
  3. Renaming a class:
    Rename class
Note: Android Studio takes care of all of the instances of the field, method or class when you execute rename.

Changing Method Signatures

What about when you’d like to change the method signature?

Android Studio can take care of changing the method signature for you by adding, removing, or reordering a method’s argument. To do so, use the key combination of + F6 on MacOSX and Ctrl + F6 on Windows/Linux.

Take a look:

Extracting Variables

All this is very cool, but do you know what would be even better? Being able to extract repeated or complex code into its own method or variable.

For example, say you have a string that’s used repeatedly in your class and you want to replace all the string’s instances with a variable. Approaching this manually would be cumbersome, to say the least.

But Android Studio simplifies the whole process into a single key combination of + + V on MacOSX and Ctrl + Alt + V on Windows/Linux:

Extracting Methods

How about extracting multiple lines of code as a method? Of course you can do this, too! Use the shortcut: + + M on MacOSX and Ctrl + Alt + M on Windows/Linux:

Extracting Method Parameters

But that’s not all! You can go even further by extracting part of the method body as a parameter to the method. You can do this with the key combination of + + P on MacOSX and Ctrl + Alt + P on Windows/Linux:

Note: You can choose to replace one or all the occurrences of the method body.