Code Folding in Android Studio
“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

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

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

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.

Using the collapse
command, we can collapse the entire region so only the region label is visible within our editor. We can also click on the small +
or —
icon on the left side of the editor to manually expand/collapse a region block.

For more on code folding, check out my latest video on YouTube
I love to meet/talk/discuss and help where I can. If you want to chat or ask a question you can follow me on Twitter, YouTube, Medium, and Facebook.