815

Is there any way of auto importing (like in Eclipse Shift+Ctrl+O) in Android Studio?

I have found only Ctrl+Alt+O which ask for each thing, and I have to press Alt+Enter to accept it.

No way to do it faster?

Arsen Khachaturyan
  • 6,472
  • 4
  • 32
  • 36
Michał Tajchert
  • 10,021
  • 4
  • 27
  • 46

11 Answers11

1296

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

  • change Insert imports on paste value to All

  • markAdd unambigious imports on the fly option as checked

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

enter image description here

After this, all unambiguous imports will be added automatically.

John
  • 901
  • 8
  • 16
Vladimir Mironov
  • 29,611
  • 3
  • 63
  • 61
  • i am not find any Setting option in File Tab, it have option import/export/ and otherSettings that also don't have any Editor Option...any suggestion. – CoDe Aug 11 '13 at 16:36
  • 26
    Use Preferences under the Android Studio menu. – Ben Wheeler Sep 03 '13 at 21:52
  • Just what I was looking for! The Studio/IDEA, GNOME and Emacs keybindings overlap a good bit. Ctrl+Alt+S maps to GNOME "Toggle Shaded State" - which quickly rolls up/down a window. Disable GNOME keybinding to allow this keybinding to work in the studio window. – dturvene Oct 11 '13 at 21:56
  • 2
    If you do this, there's a bug in Android Studio 0.4.3 and earlier where it can be aggressive about replacing "R." with "android.R." if there's an error that prevents R.java from being automatically generated. To avoid problems, you should add "android.R" to the "Exclude from Import and Completion" list; see http://stackoverflow.com/questions/21102497/why-did-android-studio-change-all-my-references-from-r-to-android-r?noredirect=1#comment31771944_21102497 – Scott Barta Jan 14 '14 at 17:08
  • 2
    While i understand this is the correct and handy way along with using the Ctrl+Alt+O to optimize them... It seems mine will not update or allow the Auto Import to work when pasting.. Only rarely did it ask me to Import...damn bugs – Angry 84 Feb 05 '14 at 01:56
  • 21
    They really need a shortcut for this, like Eclipse. I don't want to keep this setting on all the time, but sometimes I really need it. For example, when I commented / uncommented a bunch of code. – theblang May 06 '14 at 15:31
  • I did this but you are still prompted to hit Alt-Enter on every class to import it. Not exactly "auto import". – AndroidDev Jan 20 '15 at 14:42
  • It works for me, if you use that option you must use Ctrl+Alt+O instead of Alt+Intro, all it will try to import everything – D4rWiNS Aug 20 '15 at 08:08
  • Too bad, I can only +1 :( – Ich Jan 05 '16 at 21:13
  • 1
    does it optimize imports automatically ? because I need to do Command + Alt + O every time even with these options selected. – Prateek Jain Mar 30 '16 at 05:15
  • Great technique. Worked like charm – sunil Apr 17 '16 at 11:56
  • Not really a big difference.Specially the first part is nonesense.You can let it ask you and then import why put it on all? – steve moretz Apr 21 '19 at 16:11
91

These are the shortcuts used in Android studio

Go to class CTRL + N
Go to file CTRL + Shift + N
Navigate open tabs ALT + Left-Arrow; ALT + Right-Arrow
Look up recent files CTRL + E
Go to line CTRL + G
Navigate to last edit location CTRL + SHIFT + BACKSPACE
Go to declaration CTRL + B
Go to implementation CTRL + ALT + B
Go to source F4
Go to super Class CTRL + U
Show Call hierarchy CTRL + ALT + H
Search in path/project CTRL + SHIFT + F

Programming Shortcuts:-

Reformat code CTRL + ALT + L
Optimize imports CTRL + ALT + O
Code Completion CTRL + SPACE
Issue quick fix ALT + ENTER
Surround code block CTRL + ALT + T
Rename and Refractor Shift + F6
Line Comment or Uncomment CTRL + /
Block Comment or Uncomment CTRL + SHIFT + /
Go to previous/next method ALT + UP/DOWN
Show parameters for method CTRL + P
Quick documentation lookup CTRL + Q
Delete a line CTRL + Y
View declaration in layout CTRL + B

For more info visit Things worked in Android

K.Dᴀᴠɪs
  • 9,384
  • 11
  • 31
  • 39
Kumanan Android
  • 1,031
  • 7
  • 2
87

By changing the keymaps settings you can use the same keyboard short cuts as in Eclipse (Or your favourite IDE)

File -> Settings -> KeyMap

Android Studio -> Preferences -> KeyMap (Mac)

Change keymaps settings to eclipse so that you can use the short cut keys like in eclipse. For those who just want to change this one setting, it is

Main Menu -> Code -> Optimize Imports...

enter image description here

QED
  • 9,439
  • 6
  • 46
  • 82
deniz
  • 11,759
  • 10
  • 42
  • 61
  • 10
    Is there a specific item in Intelij that is equivalent to ctrl shift O in eclipse? I am not finding it. – Shawn Aug 25 '14 at 21:31
  • 7
    Ctrl+Alt+O for android studio in Mac – Benkax Dec 08 '14 at 07:54
  • 1
    *This* answer is what I was after. – QED Aug 27 '15 at 03:17
  • 3
    This answer is incorrect! The question is: is there a way to Auto Import? Your answer makes Android Studio to eliminate unused imports. The author wants to auto import a class like `com.company.bean.MyBean` automatically, when he writes `MyBean bean = new MyBean();` – shimatai Apr 03 '17 at 22:43
26

Android Studio --> Preferences --> Editors --> Auto Import

  • Checked Optimize imports on the fly option
  • Checked to Add unambiguous imports on the fly option
  • Click Apply and OK button.
Paresh Mangukiya
  • 14,668
  • 7
  • 90
  • 90
ethemsulan
  • 2,113
  • 24
  • 19
14

Note that in my Android Studio 1.4, Auto Import now under General

(Android Studio --> Preferences --> Editors --> General --> Auto Import)

enter image description here

Ajay2707
  • 5,345
  • 6
  • 34
  • 52
Quan Nguyen
  • 4,624
  • 3
  • 21
  • 25
9

You can make short cut key for missing import in android studio which you like

  1. Click on file Menu
  2. Click on Settting
  3. click on key map
  4. Search for "auto-import"
  5. double click on auto import and select add keyboard short cut key
  6. that's all

enter image description here

enter image description here

enter image description here

Note: You can import single missing import using alt+enter which shown in pop up

enter image description here

Adiii
  • 35,809
  • 6
  • 84
  • 87
7

On Windows, highlight the code that has classes which need to be resolved and hit Alt+Enter

Heinrich Cloete
  • 664
  • 5
  • 10
Chris Sprague
  • 1,789
  • 22
  • 16
  • This is what I was looking for, are there any other similar options to do this on the particular problem item rather than the class? – user1821961 Jun 18 '17 at 23:22
5

Go to File -> Settings -> Editor -> Auto Import -> Java and make the below things:

Select Insert imports on paste value to All

Do tick mark on Add unambigious imports on the fly option and "Optimize imports on the fly*

Ajay2707
  • 5,345
  • 6
  • 34
  • 52
Dhiraj Himani
  • 928
  • 9
  • 9
4

File > Settings > Editor > General >Auto Import (Mac: Android Studio > Preferences > Editor > General >Auto Import).

Select all check boxes and set Insert imports on paste to All. Unambiguous imports are now added automatically to your files.

Reference

MRX
  • 1,240
  • 2
  • 11
  • 28
3

For Linux (Ubuntu 14.04), you can go to

File -> Settings -> Editor -> Auto Import

check all the boxes and insert all imports on paste.

enter image description here

KhogaEslam
  • 1,383
  • 16
  • 18
3

In the Latest Version of Android Studio, the options for Auto-Import is enabled by default, so kudos no need to worry about that.

On Windows: If for some reasons auto-import is not enable you can go to settings by typing shortcut: Ctrl+Alt+S.

In the Search term just type 'Auto-Import' and then select 'Add unambiguous Imports on the fly' and click Ok.

That's it. You are Done. SnapShot of Auto_import

Heinrich Cloete
  • 664
  • 5
  • 10