Disappearing import statement in Android code - android

I am using "Hello, Android" 1.5& 1.6 with the 2.1 android Java build. I typed:
import android.os.Bundle;
It disappeared as soon as I finished typing!! Help, please.?

By default Eclipse hides all import statements under a single line.
Click the + button next to the import you can see and it should show them all.

Settings -> Editor -> General -> Auto Import.
You will find two options.
1. Add unambiguous imports on the fly.
2. Optimize imports on the fly.
When we type any class or annotation name and if the class is unambiguous and it is still not imported then IDE will automatically add the imports for us if Add unambiguous imports on the fly is selected. If the imports are ambiguous then IDE prompts us which class/interface need to add for import statement.
If "Optimize imports on the fly" option is turned on, when you import something then IDE will check whether it is used in the code or not. If the answer is "No" then IDE optimizes the imports and removes it. For solving this issue uncheck the "Optimize imports on the fly "option.
To learn more check the below links,
Remove unused imports in Android Studio
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206223159-Java-import-hiding-disappearing

Related

Android studio is making used import statement as unused?

I have just starting working my android studio project in another pc with same android studio version and it automatically makes used import statement as unused in the code and mark evey code related to particular import statement red. How can i fix this issue , its very annoying. but when i run the project ,it works fine without any error.
I have tried all those tricks including deleting .ide ,.gradle and even restarting with invalidate cash ,restard and disabling automatic import as well.
In the below screenshot , android studio just grayout all import statment i am using below and mark all those classes and its related method red in the code. how can i fix this issue
MyAppObserver is unused import because you import it in the same file it's declared. As for others I am not sure because I can't see full file structure.
Your interface of the same name is causing a conflict.
interface MyAppObserver
Rename your interface and the imports should work correctly, and the red marks should go away.
Try to clean then rebuild project.
If this still exists then close Android studio then re-launch it again.
sometimes I face the same situation but it got fixed after clean and rebuild
This happens sometimes. You will need to clean the project. If that doesn't work try invalidate caches/restart.
First, go to File, then choose: Invalidate Caches/Restart...
Then this dialog pops up, choose Invalidate and Restart.
Most of the times when IDE doesn't behave as it should, you can try this.
You don't need to import the interface as it is inside the same file.
You can make a separate file for the interface.

Android Studio does not rearrange import in lexicographic order

Given the following imports
import javax.inject.Inject
import kotlinx.android.synthetic.main.fragment.*
Android Studio rearrange to the following when pressing control + option + o
import kotlinx.android.synthetic.main.fragment.*
import javax.inject.Inject
Which is not in lexicographic order. My pre-commit hook running ktlint then fails because of this. Is there any way to fix this issue?
Its strange behavior but if you delete all from Import aliases separately you will get lexicographical order
Before:
After:
The latest IntelliJ IDEA / Android Studio might not be able to conform to full specifications in the Android Kotlin style guide. So, currently, there is no straight-forward way of arranging the imports lexicographic order in IntelliJ IDEA / Android Studio.
For now, you may want to disable this check for Klint by disabling 'import ordering' in the .editorconfig file in the root of your project:
[*.{kt,kts}]
disabled_rules = import-ordering
This will disable the check in your project.
IntelliJ IDEA / Android Studio doesn't let you order imports alphabetically, neither in Java nor in Kotlin.
However Java features an Import layout section, which can give you some control. But still it's impossible to have an exact ordering, as you want it.
This isn't available for Kotlin is now available in Kotlin too. You can look at https://youtrack.jetbrains.com/issue/KT-10974
You could disable import ordering for that file.
I had this:
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import java.io.Serializable
import kotlinx.android.parcel.Parcelize
Ktlint reordered it that way, but check continued to fail:
Had to add the ignore in the first line:
import android.os.Parcelable // ktlint-disable import-ordering
import com.google.gson.annotations.SerializedName
import java.io.Serializable
import kotlinx.android.parcel.Parcelize
If you are in IntelliJ/Android Studio, usually just clicking Code -> Optimize Imports is enough to fix it
That’s just a linting error if I recall correctly
I know this is old, and I did manage to fix the issue by adding // ktlint-disable import-ordering but eventually I found a better solution.
If you have command line ktlint installed, you can run the ktlint --android -F terminal command on your android source code folder and it will automatically fix this issue. The --android part is very important, using the ktlint -F command, without the android part, will fix some ktlint issues but not his one.
You can run this command on your android studio terminal. This will automatically reformat all the ktlint issues.
Cmd -
./gradlew ktlintFormat
I ran into a rare case where my android project was using ktlint v0.47.1. Everything passed when I ran the command ./gradlew ktlint locally; and ./gradlew ktlintFormat didn't find anything to change.
However, it kept failling the lint checks with the PR because it was using the latest version v0.48.2 in the Github action setup. There are fixes for importing in v0.48.
I updated ktlint to v0.48.2 in the project in order to find and fix the lexicographic order issues.

Android x databinding

I migrated my project to androidx and now I'm receiving errors for every databinding generated class because all these generated classes still use import android.support.v7 instead of android x. So far I tried to delete all generated classes and rebuild project, but it generates same ones after building project. Does anyone know how to solve it?
Even though you have migrated to AndroidX, as you said your classes are still using the old imports. Android Studio doesn't seem to refactor it well enough for some reason. This happened to me too and I had to manually delete the invalid imports and add the androidx imports. (alt + enter) to add the correct imports after deleting the invalid imports.
Edit: you'll need to change the class imports as well as the full qualified names of the widgets in the xml layout, menu, etc. files.
Change import android.support.v4.app.Fragment;
To
import androidx.fragment.app.Fragment;
Change <android.support.design.widget.CoordinatorLayout>
To <androidx.constraintlayout.widget.ConstraintLayout>

Changing package path from existing imported project

How can I change the entire package path in Android Studio, so that also the import statements within Java classes and xml files will be changed automatically.
I know there are threads already posted about this topic, but whenever I change package path, I have to adapt all import statements by hand. And it's much work when there are over 100 classes :(
If you want to move your Java classes to different package, in Android Studio you can right click on package name and then select Refactor->Move.

Android Eclipse ADT Code Errors when starting new Android Project

When I start a new Android project in the Eclipse/ADT bundle, I get 2 errors in the code..
The 2 warning messages are as follows:
The import android.app.ActionBar is never used
The import android.os.Build is never used
Both of them are in MainActivity.java
This is very odd because it happens as soon as I create a new project!
Can anyone please help me solve this problem?
These are not errors, but warnings. Chances are that these classes (ActionBar and Build) are introduced by the "New Project" wizard which generates code from a generic template. These classes may be useful when you proceed with your project. In case you do not need them, simply remove the import statements.
android.app.ActionBar works on 3.0 level 11 API,
You can do two things to resolve this error,
go to the line and remove the import statement,
or
press Ctrl + Shift + O (the letter O and not zero).
It automatically organizes the imports and fixes them.

Categories

Resources