Android Studio: Java Editor
Versão em português aqui.
Some days ago I started a series of posts about how to improve the development productivity with Android Studio. After a post about how to improve the screens’ layout preview, it’s time to talk about how to write Java code faster. In this post, I will talk about some shortcuts and options that I use in a daily basis. In a future post, I may approach some advanced features that I don’t use that often.
All the features presented in this post where tested in Android Studio 2.0, preview 9. Although they may work in different versions, differences may occur.
For the start, I use to change the expressions auto complete settings by adjusting an option in Android Studio. In the settings (ctrl+alt+S/cmd+alt+S), searching for Case sensitive completion, there is an option for to adjust if the complete options will be filtered considering the letters case or not. I use to change this option to None. This way the complete works better for me. I suggest you test the different options to check which one works better for you.
Settings adjusted, it’s time to start exploring the intentions. As we create our classes and components, Android Studio is able to foresee some actions and intentions. For example, when we make a class implement an interface and press alt + enter (return), Android Studio will suggest the inclusion of the methods from the interface. Pressing enter (return), the methods will be included with their basic structure. The alt + enter option works in many situations, usually allowing include or adjust code. In general, I use to run this option to import classes, generate local variables, implement methods from interfaces or super classes, and many other options. The best way to know the intentions is to test it as you go by.
I said a lot about the enter. Why not test the tab key too? When we use the complete option from most of the IDEs, the enter/return key selects the suggested option. At the Android Studio, this option inserts the new code inside the line. Sometimes we don’t want this behavior. To be honest, most of the times I don’t want this behavior. For that reason I press tab instead of enter most of the times. This way, the existing line of code is replaced or completed, making the code editing more productive to me.
Speaking in code completion and editing, how about making the Android Studio generate the basic building blocks of the Java language automatically? Starting with for blocks, that can be generated by typing iterable.for. The complete option will suggest the default foreach from Java language. If you want to create indexed for, just type iterable.fori. Switch blocks can be generated by typing enum.swtich. Going beyond the basic blocks, we can create new methods using alt + insert. This option shows a list of methods including getters, setters, constructors, toString, among some others. This option makes easier to create POJO or DTO classes with little effort.
I also use to refactor my code using some shortcuts. To create a method from a line of code, I use ctrl/cmd + alt + M shortcut with the code selected. To convert an local variable into an instance attribute, there is the ctrl/cmd + alt + F with the variable selected. You can also create a local variable to receive an expression by selecting the expression and pressing ctrl/cmd + alt + V. Still on refactoring, the option shift + F6 makes easy to change identifiers’ names. A list with suggested names that follow the Java conventions usually is displayed.
There are many options in the editors from Android Studio. Many of those I don’t even know or tried before. In the following posts I will approach more advanced features and shortcuts. If you use different features or shortcuts, don’t forget to comment.