Android Studio – Alt+Enter is Your Friend

Android Studio (AS) is packed full of super handy keyboard shortcuts which can make your life much easier as a developer. This post focuses on just one of these — possible the most useful of them all — Alt+Enter.

Alt+Enter is a context aware shortcut which brings up what JetBrains calls the ‘intentions’ menu. Depending on where your cursor is when you use the shortcut you get custom list of actions to help make your existence less painful.

The following are examples of the intentions which I find most useful day to day. I hope you like them too.

Simplify Nested Ifs

If you ever have one if statement which is nested inside another one this action will combine the logic of the two statements into one. The thing to be wary of with this one is ending up with super complicated if statements which reduce the readability of the code!

Implement an Interface

So you’ve written a shiny Interface to help separate your Presenter from your View? Good work you! No need to manually create an implementation class. Alt+Enter from the Interface name and AS will do the hard work for you.

Add a Missing Method to an Interface Implementation

What if you’ve already got an Interface implementation but you’ve just added a new method to the Interface itself? Alt+Enter on the red squiggly line to quickly add the missing method(s).

Regex

If you’ve got a regex stored as a string AS lets you declare it as such using ‘Language Injection’. After you have done this you can test the regex within Studio. You can also edit the regex in an editor. The advantage of this is AS will automatically escape \ characters, etc leaving you to concentrate on your 470 character long regex.

As an aside you can also declare other language types in the same way. The only other one I’ve used is SQL but who knows what others may find useful!

Bind Constructor Parameters to Fields

If you’ve created yourself a POJO and written a constructor then AS will automatically create the instance variables for you.

This is essentially the opposite of creating the instance variables first and then using Cmd+N to create the constructor.

Create a New Constant

If you need to create a new constant field — don’t do it manually. Just use the name of the constant in your code. Once you’re ready just Alt+Enter on the red variable name and you’re done.

This works for instance variables, local variables & parameters too!

Extract a String Resource

When you’re in the middle of creating an XML layout the last thing you need is to keep switching back and forth to your strings.xml. Luckily you can hard code any strings you want and then use Alt+Enter to save the day.

If you’ve got multiple language strings.xml files it will even let you decide which ones you wish to export to.

Switch Between Anonymous Class & Lambda

If you’ve joined the Retrolambda party then you may see AS highlight your old anonymous classes in grey. Using Alt+Enter you can quickly switch between an anonymous class and a lambda. It will even shorten things further into a method reference.

I find this one particularly useful when using RxJava as it allows you to expand some of the less clear lambdas out to anonymous classes to determine what they’re up to, then collapse them back down again!

Conclusion

There are many, many more intentions available in Android Studio to save both your time and sanity. My best tip to finding these is whenever you see something highlighted in red or grey simply stick the cursor there and give Alt+Enter a go.

If there are any intentions which you find particularly useful which I’ve not covered above please do leave a comment.