When you write java code in Android Studio, you always need to import some classes which you used in the android application. This article will tell you how to import all used classes automatically.
1. Auto Import Classes In Android Studio.
1.1 For Windows or Linux.
- If your android studio is running on Windows or Linux, you can follow the below steps.
- Open android studio, click the File —> Settings menu item, then it will popup the Settings diaolg window.
- Click Editor —> General —> Auto Import menu item on the left side of the popup Settings dialog.
- On the right side, choose All from the Insert imports on paste drop-down list in the Java section. And check the Add unambiguous imports on the fly and all other checkboxes. Click the OK button.
- Now when you write or paste a class name in your java source code, the android studio will automatically import the class with its package.
- If you select Ask in the Insert imports on paste drop-down list in the above Settings —> Editor —> General —> Auto Import dialog window. Then android studio will ask you whether import the class or not, you can press Alt+Enter to import it.
1.2 For macOS
- If you use macOS, the only difference is where you can configure the above settings.
- Just click the Android Studio —> Preferences menu item to open the Preferences dialog window.
- Then click the Editor —> Auto Import menu item on the left side of the popup Preferences dialog window. The subsequent settings are very similar to section 1.1.
2. Keymap Shortcut.
- There are a lot of shortcuts in android studio which are very useful when you are writing java source code. If you do not remember them, you can find all the shortcuts follow the below steps.
- Click File —> Settings menu item in android studio ( Windos or Linux ) or Android Studio —> Preferences ( macOS ), in the popup dialog click Keymap in the left panel.
- In the right panel, you can see all the keymap shortcuts listed in different categories. You can read or edit shortcuts there also.
3. Create auto-import Shortcut Key In Android Studio.
- Below are the steps for creating an android studio shortcut key combination to auto-import all missing imports.
- Click android studio File —> Settings menu item.
- Click the Keymap menu item on the popup Settings window.
- Input the search keyword auto-import on the right side search box and click the enter key to search.
- It will list the Other —> Auto-import item below the search box, double click the Auto-import item.
- It will popup a menu list, select the Add Keyboard Shortcut item in the list.
- Then it will popup the Enter Keyboard Shortcut dialog window, input the Shortcut key combination ( such as Ctrl + Shift + O) in the First Stroke text box.
- Click the OK button to save the changes.
- Now when you press Ctrl + Shift + O at the same time, it will import all the missing import classes automatically in android studio.
4. Question & Answer.
4.1 How To Use Shortcuts To Auto Import Java Classes In Android Studio.
- I am a fresh man to Android Studio, I am familar with Eclipse. In Eclipse, I can use the shortcuts Shift + Ctrl + O to import all the java packages in one time. But in Android Studio, I only find the shortcuts Ctrl + Alt + O, but this shortcut will ask me to press Alt + Enter for each java package import code to accept it. Is there a way to import all java packages at one time?
- The shortcuts that you used Ctrl + Alt + O is not wrong. What you need to do is to configure your Android Studio like this article tell you In section 1.1 & 1.2. If you use Android Studio On Windows or Linux, please refer to section 1.1, if you use Android Studio on macOS, please refer to section 1.2.