ChatGPT解决这个技术问题 Extra ChatGPT

What is the shortcut to Auto import all in Android Studio?

Is there any way of auto importing in Android Studio. Something like the auto-import feature that Eclipse has when you use the keybinding: SHIFT + CTRL + O in Android Studio?

Currently I have only found CTRL + ALT + O, which ask each individual thing, and I keep having to use the keybinding ALT + ENTER to accept.

Is there any way I can speed things up?

what do you wanna import?
Something like organize import (classes) in Eclipse.
but I'm looking for an keyboard shortcut for Android Studio.
"organise imports on the fly" is what you want, if you opened settings and in the search box type 'import' you would have found it

J
John

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

https://i.stack.imgur.com/YCJ3Z.png

After this, all unambiguous imports will be added automatically.


Use Preferences under the Android Studio menu.
If you do this, there's a bug in Android Studio 0.4.3 and earlier where it can be aggressive about replacing "R.<resource id name>" with "android.R.<resource id name>" 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 stackoverflow.com/questions/21102497/…
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
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.
does it optimize imports automatically ? because I need to do Command + Alt + O every time even with these options selected.
K
K.Dᴀᴠɪs

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


That is nice, but doesn't answer OP's question.
CTRL + ALT + L not working in Ubuntu instead its locks down the display.
Q
QED

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...

https://i.stack.imgur.com/MB1Kn.png


Is there a specific item in Intelij that is equivalent to ctrl shift O in eclipse? I am not finding it.
Ctrl+Alt+O for android studio in Mac
This answer is what I was after.
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();
e
ethemsulan

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.


Preferences --> Editor --> General --> Auto Import
A
Ajay2707

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

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

https://i.stack.imgur.com/CGAMw.png


A
Adiii

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

Click on file Menu Click on Settting click on key map Search for "auto-import" double click on auto import and select add keyboard short cut key that's all

https://i.stack.imgur.com/3jgsz.png

https://i.stack.imgur.com/Dfeqa.png

https://i.stack.imgur.com/M07YN.png

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

https://i.stack.imgur.com/WsbtJ.png


H
Heinrich Cloete

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


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?
A
Ajay2707

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*


M
MRX

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


K
KhogaEslam

For Linux (Ubuntu 14.04), you can go to

File -> Settings -> Editor -> Auto Import

check all the boxes and insert all imports on paste.

https://i.stack.imgur.com/p2W3M.png


H
Heinrich Cloete

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


A
Abey Bruck

as I have found on kotlin training from google developers, on android studio 4.2.2, On macOS, open the settings by going to File > New Project Settings > Preferences for New Projects... Expand Other Settings > Auto Import. In the Java and Kotlin sections, make sure Add unambiguous imports on the fly and Optimize imports on the fly (for current project) are checked. Note that there are two checkboxes in each section. Save the changes and close settings by pressing OK.

https://i.stack.imgur.com/j9tLt.png

In Windows, open the settings by going to File > Settings > Editor > General > Auto Import. In the Java and Kotlin sections, make sure Add unambiguous imports on the fly and Optimize imports on the fly (for current project) are checked. Note that there are two checkboxes in each section. Save the changes and close settings by pressing OK.

https://i.stack.imgur.com/990kH.png

The unambiguous imports settings tell Android Studio to automatically add an import statement, as long as it can determine which one to use. The optimize imports settings tell Android Studio to remove any imports that aren't being used by your code.