Handy Android Studio Tips and Tricks
If you use Google’s Android Studio (AS) for development, you owe it to yourself to watch out this video from the developers during Google I/O 2016: https://youtu.be/hHnTIMjd1Y8
Here’s the tips and tricks that I found the most useful:
Ctrl+Shift+a is your FRIEND
As the AS menus get deeper and more feature rich, it’s easy to forget the location of your favorite tool or command. Enter Ctrl+Shift+a! It’s a onebox-style search bar that has an index of literally EVERY command that you can issue or use in AS. Forgot how to attach to a running session? CSa. Need to wrap something in a try/catch? CSa. If you remember a single thing from this blog post? CSa. No, seriously, I want you to read the rest of this, but you could walk away now and still have your productivity dramatically increased.
1. Find where your current file is with Navigate -> Select In… -> Android / Project (or Select In… from Ctrl+Shift+a)
When you end up with a big project and you’re browsing around through various definitions, it’s easy to end up with a file open and you want to see where it lives in your package / directory structure. Either browse to the Navigate menu -> Select In… -> Android or get to “Select In…” from the CSa menu and you can pop up the current file in your left-hand navigation menu. You can also get there with Alt-F1, but my Ubuntu distribution captures that keycode for navigating the quick menu so it’s nice to have the menu option as well.
2. Tab vs Enter method completion
So, say you have a method and you want to rename it:
Object.foo() //Needs to be Object.bar()
If you just put your cursor next to the period and start typing ‘ba’, the intellij menu will figure out the ‘bar()’ method but then you need to decide how to choose it. If you hit enter, you’ll end up with
This is bad. Next time, hit ‘tab’ and then ‘foo’ will be REPLACED with ‘bar’ and you can happily avoid the forced deletion of your old method.
3. Alt+Shift+Insert for column selection It’s not an everyday occurrence, but if you press Alt+Shift+Insert and then drag or shift-select, you can select in a block rather than line-by-line. This lets you do clever things like change XML headings in a single block.
4. Alt-j for multiple selection
By using Alt-j for selection, you can intelligently select all occurrences of the word / method / variable that is currently hosting your cursor. It’s like a tactical refactor -> rename that works on string constants or anything else that you might need to edit.
5. Find what writes / reads a variable with ‘Analyze To / From Here’
Very often in a properly refactored application, you might have a single method being called from a LOT of different places in your code. If you select a variable and then Analyze -> Analyze Data To / From Here (CSa to the rescue!) you can see every piece of code that might influence or by influenced your current variable.
6. Ctrl+F12 for Method Search
You can bring up a quick version of the ‘structure’ menu in place by hitting Ctrl+F12. This menu is also a search box, so you can quickly jump to methods and inner classes with a few keystrokes.
7. Ctrl+Shift+[0-9] / Ctrl+[0-9] for bookmarking
When you’re editing in several different locations, it’s helpful to be able to jump back and forth. If you’re in the same file, it can be annoying to scroll around just to see the different locations that you’re messing with. Ctrl+Shift+Any Number will make a bookmark that you can easily navigate back to with Ctrl+The Same Number. It even works in between files!
8. Ctrl+w to select a code block (and gradually increase the selection)
When refactoring or otherwise messing with a deeply nested code block, it can be helpful to select all the code in the current context. Ctrl+w (or add a shift to reverse it) will gradually move up in the current code context to select more and more of the current block.
9. Ctrl+j for Postfix Code templates (for and forr are just the beginning)
AS provides a TON of helpful code templates available from the Ctrl+j menu. Setting visibility, adding try / catch blocks, reverse iterators, you name it. Hit Ctrl+j after a variable and just explore the (context sensitive) options.
10. Local History
So, did you try out all of my handy tips? If you did, chances are you made a mess of your code! AS keeps a local diff-style history of every change you’ve made today. Check out VCS -> Local History. Aside from backing off dumb scratch pad code, you can also use it to quickly evaluate your changes for the day and determine what you worked on in a specific project!
Thanks so much for taking the time to check out these small tips. Make sure to watch the video for even more info about debugging and the Android Studio toolset. Also, for more great tips, follow this guy:
Or watch this project where his tips are being aggregated:
Thanks for reading!