ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

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

expanded code block

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

collapsed code block of an anonymous inner class

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

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.

collapsed 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.

Check Out My YouTube Channel

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Nate Ebel

Building great software and helping others do the same.

Responses (3)

Write a response

Instead of Ctrl+ plus/minus you can use Ctrl + > key (at least on Windows).
If you use Ctrl+Shift+>, it folds the innermost block first, then moves on to the parent blocks.

--

This should be standard in every IDE. Another code hiding tip in Android Studio is to highlight an area of code you want to hide and press “CMD .” All the highlighted code turns into three dots. Great for when you’re in a class with alot of functions.

--

Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating the news article.
<a href=”https://www.gangboard.com/app-programming-scripting-training/android-training”>Best Android Certification Training </a>

--