23

I'm using Android studio and i'm trying to find a way to use a very common thing i used to do in eclipse. when i'm writing (for example) a for loop, i'm writing the word "for" then i click on Ctrl+Space and eclipse autocomplete to a for loop with all the parameters including

for (int i=0 ; i<mCheckBoxArray.length;i++){
            mCheckBoxArray[i].setChecked(false);
        }

but now, in Android studio it just auto complete to for(). the for loop its just an example, does anybody knows how to do this in android studio?

jesterjunk
  • 2,342
  • 22
  • 18
digitalmidges
  • 826
  • 3
  • 13
  • 24

4 Answers4

40

You can try fori from suggestion list

enter image description here

Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
21

More you can see like iter Iterate (for each..in) itin Iterate (for..in) itli Iterate over a List itar Iterate elements of array ritar Iterate elements of array in reverse order

jettimadhuChowdary
  • 1,058
  • 1
  • 13
  • 23
1

I found some solution.

In Android Studio, open Live Templates page. If you dont know(I didnt find it) where it is, try this.

In java code, write "fori" then press Alt+Enter, then select "Edit Live Templates Settings". You can see it in iteration section how it is done.

You can write your own templates for "Any Loop". For examples I wrote "if statement".

On iteration section or other section(whatever you want), top-right, press + icon, select Live Template, fill "Abbreviation" and description. In template text, you can search above how to make fori or ifn.

My abbreviation text is "ifi" and template text is :

if($VAR$ == true) {
    $cursor$
}

OR

My abbreviation text is "ife" and template text is :

if($VAR$ == true) {
    $cursor$
}
else {

}

Press Apply and OK, then in java code write ifi or ife, see what is happening :)

You do this for "try-catch, While, Switch, Foreach, Do-While or etc.".

hebset
  • 13
  • 1
  • 8
  • For those that can't find where: Win:`File -> Settings -> Editor -> Live Templates`, Mac: `Android Studio -> Preferences -> Editor -> Live Templates` – harlekintiger Jul 15 '21 at 10:47
1

Just use alt + ctrl + t

or type "try", then press alt + ctrl + t , then select "try / catch".

enter image description here

Anand
  • 4,355
  • 2
  • 35
  • 45