Thursday, October 8, 2015

Save time with these Android Studio features

Android Studio is a very powerfull IDE. It has features that make it robust, attractive and well fitting for the modern programmer.

But if we are going only for the templates, the visual and the additional plugins we could be missing some important aspects of the IDE. There are many refactoring items that will help you boost your productivity and save some time to watch Netflix.

Generating constructor, setter and getters for any class.


Press <Alt+Insert> to show the "Generate" menu


You write a class to handle a data structure. You find out that it has over 20 properties and according to that guy in that book back at college, properties should remain private and you should acces them via setters and getters. What a dick!

When you write a class, you can invoke the <Alt+Insert> hot key. That will open the "Generate Code menu". You can select "Setters", "Getters" and "Constructor" and it will take the properties in your class and will build all that code just for you.

Overriding methods





An Activity has a lot of implemented methods. I don't even know how many are there but I'm sure i'll find out one day.

When a class can override a method, you can see all of them by using the hotkey <Ctrl+O> (thats an "oh" not a zero"). This will show you all of the methods that you can override from the current class. You just select one and click "Ok" and the IDE will write the code for you.

Creating new methods.




You are doing a big process. The flashback takes you to college again hearing those words "Decompose your code into sub-routines so the abstraction level is low". Whatever that means you figure that you have to create a new method.

In Android Studio you can just call a method then press <Alt+Enter> once the IDE couldn't find the method on the class. Then you can just select "Create method". The editor will create the return type if theree is a way to infere it; calling the method to an int will make a method that returns an int. Try it!

Implementing interface's methods.




Whenever a class implements an interface the IDE will look for any method in the interface and will check against the current methods the class.

After the IDE checks the missing methods of your implemented interfafce, you do the <Alt+Enter> trick over the class name and ask the IDE to implement the missing methods from the interface. It will also add the "Override" annotation.

Sorround code.




When a block of code needs to handle an exception, or you need to nest it inside an <If> you can call the <Ctrl+Alt+T> to sorround the selected code to wrap it betwen an operation like Try and Catch.

This will add the necesary spaces and will keep the code format without doing a mess.

Copy and paste line.


Put the cursor over a line of code then press <Ctrl+D>. This will copy the entire line and paste it right below.

 Now you know, you can boost your code speed with a few tricks from the IDE. Give it a try!.

I will keep adding more cool features for coding in Android Studio.
Thank you for reading.





No comments:

Post a Comment