21

Is there any auto-complete shortcut or code-generation command in Android Studio that creates a stub

switch (myEnum){

}

statement containing all of the possible case statements for a defined enum as in Eclipse?

Community
  • 1
  • 1
Francois B
  • 464
  • 4
  • 14

3 Answers3

49

Put the caret on "switch", press Alt-Enter, select "Create missing 'switch' branches".

yole
  • 87,251
  • 17
  • 245
  • 186
0

Enum.class

public enum
myEnum{
Item1,
Item2,
Item3,
Item4
}

EnumSwitchImplement.class

private Enum.myEnum mMyEnum;

switch(mMyEnum){
//put cursor here and press Alt + Enter

/*a box will come with option "create missing 'switch' branches"
select.*/
}

//your switch will convert to

switch(mMyEnum){
case Item1:
break;
case Item2:
break;
case Item3:
break;
case Item4:
break;
}

This works on Android Studio. Haven't check on Eclipse. :)

Shashank
  • 153
  • 1
  • 3
0

Just place the mouse pointer over switch and then wait for some time. An yellow bulb will be shown as below. Click on that yellow bulb (or press ALT+Enter) and click on the option Create missing 'switch' branches option.

enter image description here

This will auto create the switch case-break statements as below. enter image description here

Hope this helps someone.

sunil
  • 6,226
  • 1
  • 31
  • 43