Android Studio auto importing wrong Context class - android-studio

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 :-)

Related

Android Studio: import org.tensorflow.Operation does not seem to work

I am using the latest Android Studio to create an image recognition project. I am using a .pb file downloaded from Github.
I have added "implementation 'org.tensorflow:tensorflow-android:1.5.0'" in build.gradle file.
When I go to the java file, the following two import statements are "unused"
import org.tensorflow.Operation;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;
Could somebody help? I am new to TensorFlow. Thanks!
Please ignore this question. I misunderstood what "unused" means.
Usually you don't have to add your imports manually. Android Studio shows some variants for imports and adds it automatically.
If your imports are grey and you get "unused" that means you didn't use them in this class so you can simply delete them.
It happens sometimes when you decided to write all imports before you actually code anything using these classes.
If you leave this warning nothing bad will happen but you use more memory than you actually need.

Android Studio Jetpack compose Auto imports are not working anymore

I've started today with Android Jetpack Compose. At the beginning it worked all fine. On some classes you just have to watch that you get the right package. But on some Point I've messed something up an I don't know what. I've just wanted to Import a package and then some context menu popped up, I accidentally pressed 'Ok' and since then the "Import" doesn't show up anymore when I write a class from a compose package.
There is surely an option I've turned off but I don't know what. Can anyone help me with this?
Go to file > Settings in Android Studio then search in the settings for "imports".
Under editor click Auto Import and scroll down to the bottom.
The check the two boxes for Kotlin as shown here:
.

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 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 Alt+Enter auto import settings

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.

Resources