115

The jump bar in Xcode and #pragma mark in Objective-C are very helpful and I'm looking for anything similar in Android Studio (either native or by plug-in).

George
  • 3,384
  • 5
  • 40
  • 64
ademar111190
  • 14,215
  • 14
  • 85
  • 114
  • 8
    Check out the answer here: http://stackoverflow.com/a/18740965/1758547 – Luciano Nascimento May 12 '14 at 23:20
  • 1
    possible duplicate of [Java equivalent to #region in c#](http://stackoverflow.com/questions/2344524/java-equivalent-to-region-in-c-sharp) – Reed Jun 09 '14 at 22:14
  • you can check my answer here https://stackoverflow.com/a/47565502/5381331 – Linh Nov 30 '17 at 03:41
  • 1
    Since you didn't specify the language, it's also worth mentioning that Android Studio does support `#pragma mark` in C++ code. – Grishka Nov 17 '18 at 23:01

3 Answers3

149

In Android Studio you can add regions using the steps below

  1. Open the IDE you are using
  2. Select the code you want to group
  3. Press Ctrl + Alt + T (Cmd + Opt + T for mac) and select the “region .. end region comments”
  4. Now the code is surrounded with “//region” / “//endregion” lines, you can see that the region can be fold-able
  5. You can now edit the description of your group
  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

See this

Community
  • 1
  • 1
Matteo Basso
  • 2,694
  • 2
  • 14
  • 21
  • 18
    Cool, I wish it showed in the Structure view (CMD + 7) in bold like it did in the Xcode dropdown but there's always going to be development tool differences. – Stu P. Dec 14 '15 at 20:01
  • 4
    where can we suggest for what @StuartP's talking about? I mean a way to add it in Structure View. – MiguelHincapieC Jan 17 '17 at 19:30
  • 1
    Since Android Studio is based on Intellij IDEA, and since this feature is coming to IDEA, hopefully we just have to wait for a future release of Android Studio to see regions in the structure view! Here's the YouTrack issue: https://youtrack.jetbrains.com/issue/IDEA-129599 – Tim Pesce Feb 17 '17 at 17:56
  • 2
    In Android Studio 3 it (regions) do show in the structure view - which is great, though I'm finding it a bit buggy. – Tom Nov 06 '17 at 16:15
  • Is it possible to have the structure view show the region expanded by default? Looks like it always collapses the signatures on startup. – qix Mar 21 '18 at 06:08
  • @StuartP. it shows in Cmd+F12 (structure view popup) for me, times have changed it seems :-) – Dalibor Filus Jul 16 '18 at 13:10
18

Meet custom folding regions, the IntelliJ IDEA (where Android Studio is derived from) way to customized code structure support (that is what jump bar serves in Xcode).

Keyboard shortcut ⌥⌘T or manual typing,

// region REGION_NAME
// YOUR CODE HERE
// endregion

Here is the missing gif demo (that people want in their official doc) showing how Android Studio behaves when two customized regions are added.

demo

Official Documentation

https://www.jetbrains.com/help/idea/code-folding-settings.html

George
  • 3,384
  • 5
  • 40
  • 64
  • Note that the line above a new region or region end cannot contain // as it will then not be recognized. Also, whenever you change any region, the structure box will collapse all current regions. Not very "intelli". – RunLoop Jun 29 '22 at 03:51
7

No idea why answers by Android Developers mostly lacking clarity. Here is my contribution.

Simply said, just wrap the codes with

//region SECTION NAME
...
//endregion

enter image description here

GeneCode
  • 7,545
  • 8
  • 50
  • 85