Android Studio : Kotlin autocomplete not working properly - android

Kotlin autocomplete in android studio isn't showing all available suggestion.
The same code snippet in Java suggests setErrorEnabled()
Kotlin autocomplete snippet
I tried restarting IDE, invalidate cache but still don't work as it used to do in Java.
How do I fix this?

Use textPasswordInput.isErrorEnabled = true. Kotlin converts Java getters and setters into Kotlin properties. It still allows you to call setErrorEnabled but since the property syntax is preferred, it doesn’t include the setter method in the autocomplete. Notice your use of the setter is highlighted with a warning.

Related

Android studio kotlin, cannot directly select ID without findViewById()

I'm new to android studio and I'm leaning it from this video
At 50:45 he says that we can directly use the id without findViewById (), for him it shows just import that Id, but when I tried the same I'm not getting what he is getting. Can anyone tell me what's wrong, is that feature removed in the latest update??
"kotlinx.android.synthetic is no longer a recommended practice. Removing in favour of explicit findViewById"
So, this feature`s been deprecated almost a year ago.
The new recommended way of working with view tree is View Binding.
Or you could write some lazy extension function that uses findViewById under the hood.
You can use the Kotlin Android Extensions check here or with the help of data binding or view binding you can just use the id directly in to your java activity class
The way you're talking about is Kotline Extensions for view, for which you apply it in the build.gradle file using apply plugin: 'kotlin-android-extensions', sync the project as prompted by the Android studio and it starts working.
But, Kotlin extensions has also been deprecated for view binding which is null-safe, also easy to implement and better.
To read about view binding, read the Android documentation on ViewBinding here and about migrating here. You can also go with this article to help with setting up Android view binding.

Error while trying to use .startsWith() and .contains() methods in kotlin

When I try to use the methods .startsWith and .contains with the variable value, I get the words in red, because apparently, they do not exist.
Can someone help with this error? I tried looking on the internet to see if this is an old deprecated function but I did not find anything apparently it is not deprecated...
Click to see the error here
I think you are leaving an space after calling the contains function. So is like you are not sending any arguments to the function.
It should be like this:
value.contains("/")
And same for startsWith
value.startsWith("-")
I just updated my old Android studio from 4.1.2 to the new ARTIC FOX 2020.3.1... and the error magically disappeared...Thanks.

Can't use basic kotlin function like "until" in a for-loop or for each

So to not repeat my initial question that might give some background I'll just attach a link to it
So I currently need to iterate through a ChipGroup, and when I try to introduce "for each" or "until", android studio gives me an unresolved reference notice.
I've checked and double checked and I don't think I am writing it in a bad way, but the studio doesn't let me use it for some reason.
The rest of my code which is entirely kotlin is completely fine.
questionButton.setOnClickListener {
questionChipGroup.forEach
for ( i in questionChipGroup until questionChipGroup.childCount){
}
postQuestion(
questionTitle.text.toString(),
questionDetails.text.toString(),
questionTags.text.toString(),
System.currentTimeMillis().toString()
)
}
In the examples above, both until and forEach are red and are basically unrecognized by Android.
I tried to invalidate the cache and restart and that didn't help.
Am I missing something?
You are using ChipGroup API in android. It is basically a sub-type of ViewGroup, so to iterate through a ChipGroup.
for ( index in 0 until questionChipGroup.childCount){
val childView = questionChipGroup.getChildAt(index)
// Process childView here
}
forEach in Kotlin only works on Iterator. Because ChipGroup does not implement Iterator interface, that why you cannot use forEach on it.
until in Kotlin is not an infix function, but also an extension function. It only works on Byte, Short, Int, Long type, that why you cannot call it from questionChipGroup (its type is ChipGroup).
I know this is an old question. Still I am updating my answer so it may help somebody.
Check if your project supports Kotlin. Also, your Android Studio Kotlin plugin and the Kotlin runtime/compiler that you use in the project should match.
Update the IDE Kotlin plugin version to match the project by doing this: Tools -> Kotlin -> Configure Kotlin Plugin Updates -> Check for updates now
Check this to know various ways to use until

Kotlin EditText show/hide password programmatically

I know how to do this on Java.
I copied and paste the code from Java to Kotlin and Android Studio changed to this:
auth_password_text.setInputType(InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD)
But I'm receiving a message (grey not yellow like warning)
I search and found only java and react native answer but not was the best/right way to do this in Kotlin.
Thank you in advance!
In Kotlin you can set some properties accessing directly variable instead of calling setter method, that's the meaning of the warning you are getting.
You can remove warning like this:
auth_password_text.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
For any warning or suggestion reported by the IDE, you can press Alt-Enter, and in most cases (including this one) the IDE will suggest a quickfix that will automatically apply the suggested change.
In this case, it will change the call of a Java setter to a Kotlin property access:
auth_password_text.inputType = ...

Various questions #NonNull, #NotNull and #ParametersAreNonnullByDefault

I'm an Android dev who is using AndroidStudio or IntelliJ IDEA.
I tend to trust my IDE and I'm annoyed with next facts:
In android project when IDE autogenerates a method in java that extends Kotlin class then both IDE uses #NotNull instead of #NonNull, is there setting for this? This
We have #ParametersAreNonnullByDefault but when I override the method from the point 1 and I don't put any annotation IDE warns me, but why?
Am I wrong in my assumptions?
Are there solutions?
Which annotations to use for null/not-null is set under Configure annotations... in the Compiler page of the Settings/Preferences dialog. Select the one you want and press the checkmark button. See https://www.jetbrains.com/help/idea/nullable-notnull-configuration-dialog.html for documentation.
I can't test right now whether IDEA/AS use the default annotations from there when overriding a method which already uses another, but if they don't you'll need to file a ticket.

Categories

Resources