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 feature

1. Open the IDE you are using

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

Code selection

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

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

5. You can now edit the description of your group

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

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.

Exit mobile version