19

I don't know what the exact name is for s feature like this, but I will just describe it.

I am using android studio and I added some java classes from a different project. Now it says

"missing package statement: yada yada". But there is not option in the error popup to just automatically add the missing package statement. I know it seems lazy to not type it, but I like to do things quickly.

When there is an error like this, is there a way for android studio to automatically added the needed lines of code like eclipse would?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Scorb
  • 1,654
  • 13
  • 70
  • 144
  • 13
    Alt+Enter is a shortcut for Quick fix in Android Studio . Have you tried that ? – Piyush Agarwal Feb 21 '14 at 21:33
  • I believe so yes. It not not give the option to add package. Maybe this is just lacking. – Scorb Feb 22 '14 at 00:17
  • 5
    Click on the class name and than press Alt+Enter. It should give the option to add psckage declaration . – Piyush Agarwal Feb 22 '14 at 05:33
  • I agree, Android Studio does a poor job at automatically fixing stuff ! For example it's highlighted my code with the inspection error `Unhandled exception: android.content.pm.PackageManager.NameNotFoundException` and I'm thinking to myself: "ok bloody put some skeleton code down for me!" and there's no way to automatically wrap the offending line of code with a try...catch... /LAME – Someone Somewhere Jan 28 '15 at 23:43
  • The same deception here. Too bad that Google enforces us to use that product, while it's way behind Eclipse when it's about refactoring and fixing stuff. Eclipse is wonderful at it, and Android Studio just... sorry about that... sucks. :( – Orabîg Jan 30 '15 at 04:30

2 Answers2

9

This is a common frustration. While not a perfect solution, I found https://stackoverflow.com/a/16616085/967131 to work for me.

For Windows/Linux, you can go to File -> Settings -> Editor -> General -> Auto Import -> Java and make the following changes:

change "Insert imports on paste" dropdown value to "All"

check "Add unambiguous imports on the fly" option

On a Mac, do the same thing in Android Studio -> Preferences

That way, as you type, or when you copy and paste, many imports will be added automatically. Those that aren't are ambiguous; put the cursor on the class, press alt+enter and select which version of the class you meant.

Community
  • 1
  • 1
Chad Schultz
  • 7,770
  • 6
  • 57
  • 96
  • Yup. This option is almost always best paired with judicious use of "Code -> Optimize Imports" or, more dangerously, enabling auto import optimize on save. Otherwise, your code can get cluttered with unused imports during heavy development. –  May 17 '17 at 18:59
0

For Windows/Linux, you can go to File -> Settings -> Editor -> General -> Auto Import -> Java and make the following changes:

change "Insert imports on paste" dropdown value to "All"

check "Add unambiguous imports on the fly" option

On a Mac, do the same thing in Android Studio -> Preferences