I have heard before about this “ability” of Android Studio of having multiple cursors active at the same time, but I haven’t found it very useful until today. You can see what I am talking about in the gif below:
Press ALT + SHIFT and left click on the mouse and select the position of each cursor.
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.
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 😀
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)
Name Of the class : line number
As you can see, it’s not even necessary to write the whole name of the class, which is kind of cool 😀
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)
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.
1. Open the IDE you are using
2. Select the code you want to group like in the image below
3. Press Ctrl + Alt + T and select the “region .. end region comments”
4. Now the code is surrounded with “//region” / “//endregion” lines, you can see that the region can be fold-able
5. You can now edit the description of your group
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
As you can see from the first and last images, the class body is reduced dramatically and it can be better organised.