Android Studio Tip #5 – Autocomplete “for” statement

When you want to write a for statement most of you guys (or at least me :P) start to write character by character “for (int i = 0; i < list.size(); i++)”. Well Android Studio makes this easier for us and we can avoid writing that much by using a combination of 2 keys:

CTRL + J

If you are inside a method and you want to generate a for statement you just have to press CTRL + J and select fori option from the dialog. You will just have to fill in the size of the list.

fori

 

As you can see, there are more options from where you can choose. So you can generate iterators and other stuff, not just the for statement. So please feel free to try more of these options 😀

Android Studio Tip #4 – Automatic Negation

Sometimes, (in fact, many times :P) you have to negate boolean expressions, and in order to do that, you write the expression and after that you go back to the beginning to add “!”, am I right? 😀

Well, you can avoid doing this, by pressing the “!” instead of Enter when you use autocomplete. Just make sure the option from the autocomplete dialog is selected (it must be blue).

Negation

 

Android Studio Tip #3 – Open Class At Specific Line

Did you know that you can open a class at a specific line? This is very useful if you do code reviews and you want to open a specific class to a specific line. Well, Android Studio let us to achieve this by using:

To search for a file press:
CTRL + SHIFT + N ( on Windows) or Cmd + Alt + O (for MacOS)

and

Name Of the class : line number

jumpToClass

 

As you can see, it’s not even necessary to write the whole name of the class, which is kind of cool 😀

Android Studio Tip #1 – Extract Constants

If you have hardcoded strings that should be extracted into constants, you can do that in at least 2 ways:

  • the hard way
  • the EASY way

The hard way  is to write yourself the constant and then replace it in the entire code where the string is used. And the EASY way  is to use the power of Android Studio 🙂 So in order to extract a constant you have to do the following steps:

  • select the string that needs to be converted into a constant
  • use CTRL + ALT + C (on Windows) or Cmd + ALT + C (on Mac OS)

extract constant

Android Studio / IntelliJ IDEA code regions

If you ever worked with Xcode then you must know the power of “#pragma mark” macro. You could just define regions of code using that and sometimes it helps when you have classes that can get a bit big or if you just want to have a nice structure of your class.

Well you can do that on Android Studio or IntelliJ IDEA or any of JetBrains’s IDE.

Using the steps below you can group your code and name the group however you like and also contract/expand the groups of code.

Code without grouping
Code without grouping feature

1. Open the IDE you are using

2. Select the code you want to group like in the image below

Group selection
Code selection

3. Press Ctrl + Alt + T and select the “region .. end region comments”

Group selection dialog
Group selection dialog

4. Now the code is surrounded with “//region” / “//endregion” lines, you can see that the region can be fold-able

Initial code region
Initial code region

5. You can now edit the description of your group

Collapsed Group with description
Collapsed Group with description

6. You can easily see the code within your group without having to expand the group, just hover the group description with the mouse pointer

Group code preview
Group code preview

As you can see from the first and last images, the class body is reduced dramatically and it can be better organised.

See more about this here.