Android Studio Tips and Tricks
- Getting Started
- Code Refactoring
- Using Shortcuts to Move Code Around
- Shifting Code Up and Down
- Moving Sections of Code
- Duplicating Code
- Deleting Code
- Renaming Code Constructs
- Changing Method Signatures
- Extracting Variables
- Extracting Methods
- Extracting Method Parameters
- Inlining With Shortcuts
- The Magic of Quick Fix
- Using Android Studio to Navigate Your Codebase
- Searching the Codebase
- Running More Specific Searches
- Showing Methods and Properties
- Go to an Implementation/Declaration
- Locating Errors
- Debugging Like a Pro
- Color Code Logcat
- Logging Using Non-Suspending Breakpoints
- Mastering the Android Studio UI
- Grouping Your Projects
- Avoid Jumping to Design Tab
- Using Better Fonts
- Plugins: Power-Ups for Android Studio
- Helpful Plugins for Android Studio
- Miscellaneous Tricks
- Create Command-Line Launcher
- Including On-Demand Dependency
- Using Android Studio’s Local History
- Verifying Java-Kotlin Interop
- Copying Tooltips
- Keeping Your Code Tidy With Region Blocks
- Executing Code Blocks
- Sharing Your Project as a ZIP File
- Where to Go From Here?
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.
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. :]
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.
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 File ▸ Import 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.
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:
- On MacOSX: ⌥ + ⇧ + ↑ and ⌥ + ⇧ + ↓
- On Windows/Linux: Shift + Alt + ↑ and Shift + Alt + ↓
- On MacOSX: ⇧ + ⌘ + ↑ and ⇧ + ⌘ + ↓
- On Windows/Linux: Shift + Ctrl + ↑ and Shift + Ctrl + ↓
- For lines of code:
- For methods:
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:
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:
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:
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:
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:
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:
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: