Android Studio Alt+Enter auto import settings - android-studio

I have a class in my Android Studio project called Ranking. I want to use this class in a different package in the same project, so I have to import the class. Normally I would type in "Ranking" and when it turns red, put my pointer on it and press Alt+Enter. This would bring up this menu:
After hitting Enter it would ask which class to import:
At some point I accidentally hit some keys while doing this for the Ranking class. I saw a window open and immediately disappear. This presumably changed a setting somewhere.
Now, whenever I use Alt+Enter this class is imported immediately without asking which one to import:
android.service.notification.NotificationListenerService.Ranking
When I hover over the class name, it doesn't say "Multiple choices" anymore as it does for Date:
I cannot find the setting. Does anyone know where it is?

on above setting select ask instead of All....

I just found it!
The option is under File -> Settings and then Editor -> General -> Auto Import
Here you can define classes that should be ignored by the Alt+Enter auto-import.

Related

Android Studio doesn't show import suggestion

After trying adding the GSON library with "compile 'com.google...' ",I get a situation that when I try to import modules the IDE suggest me nothing except static and *. So I have to type everything. How can I fix it? I created new projects and still get this problem.
We're in 21 century and using modern IDE Android Studio. Why do you want to import packages manually just like we import when writing code in simple Notepad or any other text editor?
Just type the name of class which you want to use in your code. And the IDE is smart enough to show the suggestions which relates to your typed keyword.
I get a situation that when i try to import modules the IDE suggest me nothing except static and *.
Yes, it's indeed. Here is my screenshot
How can I fix it?
Very simple. Just type class name whatever you want and select it from suggestions. Android Studio automatically imports it.
For example (as per your requirement) you want to import #SerializedName() annotation. Type #Ser and hit Ctrl + Space and see the magic.
Now the Android Studio showing me the suggestions which matches with keyword Ser and I'll select SerializedName (com.google.gson.annotations.SerializedName). That's it.
Hope it'll be helpful.

Android Studio auto importing wrong Context class

Android studio has been auto importing io.realm.internal.Context instead of android.content.Context and its getting very annoying. I have to highlight and manually type the import statement because Android Studio continues to auto import the wrong class. Is there any way to tell the IDE to not auto import certain packages? I never use the Realm context class, and whenever I reference Context for the first time it always erroneously imports that class.
It is possible to tell the IDE to not auto import certain packages. Please refer following steps:
In Android Studio go to
Settings>Editor>General>Auto Import
In the dialog box that opens, type the name of the class or a whole package to be excluded, and click OK.
Use new (Alt+Insert) and delete (Alt+Delete) to manage the list of classes and packages that IntelliJ IDEA shouldn't place to the suggestion list.
Apply changes and close Settings/Preferences dialog.
This will surely help :-)

Android Studio imports

I'm creating a new project in Android Studio. I'm used to development in Eclipse, and I can simply hover on symbols that are not resolved and I'm able to import the dependencies. Now when I hover on the symbols, I'm simply informed "Cannot resolve symbol X" with no option to import the dependency, even for simple ones like TextView. How do I auto resolve/import these symbols?
Thanks.
One way is to place the cursor anywhere inside a class name (either by clicking with the mouse or moving the cursor with the arrow keys) then press Alt-Enter or right click. Usually the first option will be to import the class. You can also press Ctrl-Alt-O to optimize imports. I know this will remove unused imports and I think it will add missing imports.

Android studio auto fix

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

Android Studio/Intellij Idea: "Table of Contents" for a class

I have been messing around with Android Studio and so far I like most of what I have seen. One thing that has been annoying me though is this lack of "Table of Contents" for a class. I apologize for not knowing exactly what to call it. But what I am referring to is the dropdown menu in eclipse that lists all the methods, interfaces, classes and so on that are in that class file. This then allows you to jump to that position. This view is when you are in "Package Explorer" and click the arrow to the left of the class. This is one thing that makes me really miss eclipse. I know that you can easily search with Ctrl+F inside a document but I often forget the method names. I tried looking through here but to no avail. Just wondering if anyone knows some way to handle this.
IDEA has a tab called "Structure", which shows all the methods, fields, etc. of the currently-open class.
I've just got a Tip of the Day popup in Android Studio helping with exactly this problem.
You can quickly navigate in the currently edited file with
Ctrl/⌘+F12 (Navigate | File Structure).
It shows the list of members of the current class. Select an element you want to navigate to and press the Enter key or the F4 key. To easily locate an item in the
list, just start typing its name.
Also, as danny117 points out, you can use Alt/⌘+7 to show / hide the same content in a side panel view (shown above in Chris Jester-Young's answer).
View > Tool Windows > Structure
In addition to what Chris Jester-Young said, it's worth pointing out how to see the methods and properties of a class pointed by the cursor.
a) Type Ctrl + H in the class pointed by cursor.
b) In the class name, in the hierarchy window, double-click the class name. The system, upon confirmation, will decompile and open the class code.
c) Alt + 7 (Windows) or Command + 7 (MAC) to display the structure window.
d) One can now visualize properties, methods, derived classes, derived interfaces, and even include inherited items. All related to the class under the caret.
I'm currently using Beta 0.8.9 of the Android Studio and what you need to do is click on the settings icon in the Android Project View. If you select 'Show Members' then the Classes become expandable and you can navigate around the class using the project view.

Resources