DEV Community

Nate Ebel
Nate Ebel

Posted on

Code Folding in Android Studio

Check Out My YouTube Channel

“Focus On What Matters”

When working in our IDE, at times it’s easy to become overwhelmed with all the code on the screen. To better focus on what’s most important, it can be useful to hide non-essential blocks of code from our editor window.

The Code Folding shortcuts in Android Studio help us do just that.

The allow us to fold Code Blocks & Region Blocks so we can have more fine grained control over what code we are looking at in any given moment.

Shortcuts

code folding shorcuts

code folding shortcuts

The expand & collapse shortcuts allow us to quickly show or hide code blocks or code regions.

We can expand all or collapse all by adding shift to the shortcuts above.

Code Blocks

We can take an expanded block of code like this anonymous inner class

expanded code block

expanded code block

and collapse it within the IDE, using the collapse shortcut, so it visually looks like this.

collapsed code block

collapsed code block

We lose the immediate view of the code, but if that code is not relevant to us in this moment, then it can actually help reduce the visual noise in our IDE.

To collapse a specific block of code, place your cursor within the block and execute the shortcut.

If you run the shortcut multiple times, the IDE may continue to collapse blocks of code in increasing scope until everything in the current file is collapse. The same logic applies when expanding code as well.

Region Blocks

We can define custom region blocks using comments. These regions can be helpful for documenting & group related sections of code; such as the implementation of an interface.

expanded region block

expanded region block

Using the collapse command, we can collapse the entire region so only the region label is visible within our editor.

collapsed region block

collapsed region block

For more on code folding, check out my video on YouTube


I love to talk Android, tech, or geekery, so if you have feedback or want to chat you can follow me on Twitter, here on dev.to, or check out my blog

Check Out My YouTube Channel

Top comments (2)

Collapse
 
shashiprasadkushwaha profile image
Shashi Prasad Kushwaha

//region ${ Your Comment }
....
....
//endregion

This way of "Code folding" is best way to fold as:

  • Can fold any method,
  • Fold statements within any method, or any loop (ex. if,else,for) in case you have many statements inside any method/loop.
  • In the top of classes we define many Variables and if you have very big class there will more-n-more variables added day-by-day as class grows, it will good if you group all the definition in one-or-more 'region'.
  • The best use I found as if you review code of many developers in various projects and there is too many things to remember with requirements. Then, its a good practice to "Group all related methods in One 'region/endregion' with a good description. Its really helpful for next developer/reviewer/lead/manager to change/modify the code where it helps as not to wander/lost in whole file and scroll here-n-there/top-to-bottom and lose something in between.
  • Well, many of us use method separator(in Android Studio) and defining a rule for everybody/team as to use, line gap/space to grouping the similar statements within code block(method/loop) is not helpful and tough to consider/follow, in that case, this 'region/endregion' rule comes very handy.

You can use this in Visual Studio for C# as well.

And, Thanks Nate : for gathering all these Tips/hacks at one place.

Collapse
 
jaffar_khalid_a6e3702a669 profile image
Jaffar Khalid

Thanks Nate