There are two things that you should know about me:
- I am an IDE enthusiast
- I am a productivity geek
So two years ago, when I switched to Intellij IDEA, on which Android Studio is based, I spent a lot of time looking for shortcuts and techniques to become more productive. Since you are here, I suspect you might be doing the same so I’ll try to make it easier and faster for you!
In this series, we will go from very basic productivity tricks that everyone should know to more advanced topics in Android Studio.
About Keymaps
Android Studio provides different keymaps (the mapping between shortcut keys and an action). You can see which keymap you are using in Settings->Keymap.
It wouldn’t be practical to list the shortcuts for every keymap so the following will be used:
- Windows: Default
- Linux: Default
- OSX: Mac OSX 10.5+ (not the default one! Jetbrains and I highly suggest that you use this one!)
Moving Around
We spend a lot of time navigating in our code base, let’s try to do it more efficiently.
Opening Class/File/Symbol
Open class
Imagine that you must go to a class named “MainActivity”, just use this shortcut and start typing “MainA”.
Open File
Works like the “Open Class” shortcut but on every files in your project. It is very useful to open your AndroidManifest.xml or anything that sits in the res/ or assets/ folder.
Open Symbol
A very powerful but little less known variation of the previous tips: You can use this to go directly to a method or field by searching its name!
For example, if you know that you have a method named getFormattedDate() somewhere in your project, you can type it in the Open Symbol dialog to go directly to it.
Tips
Partial Matching
You can enter incomplete strings and it will work. For example. if you are searching for a class named “ItemDetailFragment”, you can actually type “IDF” and it will find it.
Line Number
Imagine that your colleague just told you that the juicy part is in ExcitingClass at line 23. You can open the file directly by appending a “:” to the class name in the Open Class dialog. e.g.:
ExcitingClass:22
You can also combine it with partial matching and type something like:
EC:22
Recent Files
Recently opened files
This will show a popup listing the files you navigated to previously.
Recently edited files
Same as the above but listing only files that have been edited.
Tip:
Start typing to filter the list.
Navigate Back/Forward
To understand this shortcut, think about how the back and forward buttons work in a web browser. Now, instead of thinking in web pages, think about source code! So when you drill down in code or open a new file, the IDE will remember where you where before, allowing you to quickly go back.
Last Edit Location
This is a variation on the “Navigate Back” shortcut that cycles between the locations where you typed something.
Picture yourself fixing a nasty bug. You think you have the solution so you start fixing it but then realize that you have to look at the android source code and a couple other classes in your project. You enter a function, which leads you to another class, which leads you to another thing and 20 steps later, you finally have the insight needed to complete your fix… but in which file and at what line where you again? Just use this shortcut and you are right back at the exact line where you stopped writing.
Show Usage
In a Persistent Panel
Shows where something is used. For a class member, it will show where reads and writes are made. For a method, it will show where it is called. For a class, it will show where new instances are created.
You can use the arrow keys and the return key to navigate trough results. You can then press the escape key to go back to the editor.
In Place
Same as before but shows the information in a popup.
Goto Declaration/Implementation (Drill Down)
Here are three shortcuts to drill down into a symbol:
Goto Declaration
Goes to the declaration of the class, method or variable. Mostly useful on classes and methods since it redirects to the implementation.
Goto Implementation
Shows a list of all the classes/interfaces implementing the selected class/interface. Also works on methods to find where they are implemented/overriden. On variables, it has the same effect as Goto Declaration
Goto Type Declaration
When the caret is on a variable, it will go to the declaration of its type. For example, if I have the following line:
Developer phil = new Developer("Phil");
If the caret is on the variable “phil”, this shortcut will go to the declaration of the “Developer” class.
Goto Super
This will open the parent of the current symbol. Pretty much the opposite of Goto Implementation. If the cursor is in an overriden method, it will open its parent implementation. If the caret is in a class but outside the method or on the class name, it will open the parent class.
That’s all folks!
In the next article, we’ll take a look at more Navigation shortcuts!