3

In Eclipse when I press Ctrl + 1 on a method parameter it gives me an option to assign that parameter to a field of the same type.

In following example, after pressing Ctrl + 1 Eclipse would suggest assigning methodParameter to the mClassField.

private class Aclass {

    private RandomType mClassField;

    public Aclass(RandomType methodParameter){
    }
}

Is there a shortcut for this in Android Studio, please?

There is only an option to Create Field for Parameter 'xxx' after pressing Alt + Enter which is not what I want. I'd like to assign the parameter to an existing field.

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70

2 Answers2

4

This only works in Android Studio as long as the method parameter and the field have the same (or close enough) names.

In the mentioned example Alt + Enter would suggest assigning the parameter to the field only if the name of the field was mMethodParameter (or the name of the parameter was classField) as shown here:

private class Aclass {

    private RandomType mClassField;

    public Aclass(RandomType classField){
    }
}

Alt + Enter would indeed offer to generate following:

private class Aclass {

    private RandomType mClassField;

    public Aclass(RandomType classField){
        mClassField = classField; // generated code
    }
}

Eclipse works a bit better in the way that it considers types too (not only names) when giving you this option.

EDIT: Some other useful shortcuts you can use for automatic assignment:

  • Cmd + Alt + V to convert selected expression to a variable
  • Cmd + Alt + F to convert selected expression to a field
  • Cmd + Alt + C to convert selected expression to a constant
Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
0

I don't exactly know what you mean but by searching something related to your question I have found few things that can make your life a lot easier when working in Android Studio. Here are some resources I found useful:

How can I auto implement suggestions in Android Studio?

Android Studio shortcuts like Eclipse

Android Studio Tips and Tricks: Must Read

http://developer.android.com/sdk/installing/studio-tips.html

If you really love to implement eclipse shortcuts on Android Studio

Android Studio shortcuts like Eclipse

Community
  • 1
  • 1
metior
  • 365
  • 1
  • 4
  • 16