Android Studio Regions — IntelliJ IDEA

Alfred
ProAndroidDev
Published in
2 min readAug 27, 2020

--

Cold folding is a good practise while working on complex code. It helps us to be focus on the task ahead and not get distracted by unnecessary information. It also helps in organising code or hiding less useful information so one can focus on more important information.

In this blog we will focus on Regions, a small topic in code folding.

Defining custom Region block

The syntax to create a custom regions starts with a comment followed by the keywordregion and end block with the comment endregion. This can be very helpful in grouping of code and also for documentation.

Syntax: Java/Kt file

//region $NameOrDescriptionOfRegion
someCodeHere()
//endregion

Here is an example of a region expanded and collapsed.

region expanded VS region collapsed

Syntax: Xml — (Oh yes ! 🤗 )

Adding regions on xml helps to maintain a clean structure also on the xml level. It makes your xml beautify on the collapsed view. It’s addictive and very very useful.

<!--region $NameOfRegion--><SomeView/><!--endregion-->

Here is an example of a layout file. The region can be added to any type of XML file.

fragment_sample.xml
Strings.xml

FAQ

  • Is there a “region-like” folding feature for xml files in Android Studio?
    - YES, there is
  • Can regions be nested ?
    - YES, they can be nested
  • Is there a limit to number of regions being created in a file
    - There should be no limitations on regions as they are linked to the comments syntax

💡 Having good code folding will serve as an extra layer of code encapsulation.

Xml regions are far less used compared to the Java/Kt regions.
Use regions in xml to precise content and to have an easily understandable code.

TL;DR

Use regions in your java/kt and xml file and keep your code clean and tidy 🧹

--

--