Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio For each Macro

On eclipse, when I write for I get a macro option on autosuggestion to implement a foreach with any list that I Have. Have any way of do it on Android studio? because when I write foron android studio I get only the traditional for and methods on autosuggestion box.

like image 477
ademar111190 Avatar asked Feb 25 '14 19:02

ademar111190


3 Answers

Android Studio do it in a different way :

you can use following 'live templates' to generate several types of code snippets for loop iteration :

iter    Iterate using foreach loop 
itli    Iterate over a List using normal for loop
itar    Iterate elements of array using normal for loop
ritar   Iterate elements of array in reverse order using normal for loop

Enter any of the above keyword in Android Studio and press Tab key, Studio will generate template for you .

Ctrl+J will give you all available templates in windows.

There are probably many more, just lookup 'Live Templates' in help documentation of Intellij Idea.

like image 189
Piyush Agarwal Avatar answered Nov 14 '22 15:11

Piyush Agarwal


Android Studio has really nice live templates that come from IntelliJ. On Mac the default keybinding is command-J, but if you're not on Mac, then search your keybindings for Insert live template...

After you press the key combo to start inserting a live template, it gives you a list of templates on the screen and you can start typing to choose a template from the list. If I want to iterate over a Iterable, I press Command-j followed by iter. It's pretty smart about reading the context and choosing likely defaults for what I want to iterate over, but it has on-screen prompts that let you enter the parameters for the template.

See https://www.jetbrains.com/idea/webhelp/live-templates.html for more info.

like image 27
Scott Barta Avatar answered Nov 14 '22 16:11

Scott Barta


This is already available in Android Studio, known as Live Template.
You can have a look at the predefined in Preferences -> Live Templates.
There is also on for a foreach loop, just type:

iter and then press ctrl + j

like image 6
Stephan Avatar answered Nov 14 '22 16:11

Stephan