I have migrated my project to Kotlin 1.3 and then it is showing this
Was that means we have to use the again the long and old fashioned setOnClickListener method or they have introduced some another method. And why they are dropping it?
The 0.10.7 release has two satellites 0.10.7-eap13 and 0.10.7-rc13
which are meant to be used with Kotlin 1.3 and release
kotlinx.coroutines. Feel free to try them and provide any feedback.
Github/Kotlin/Anko
I just tried "0.10.7-rc13" instead of "0.10.7" and it's working for now. You also have to update imports from "sdk25" to "sdk27".
Related
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.
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.
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
I want to change targetSdkVersion of my android project from 25 to 27.
The problem is that i used Kotlin and there are some big changes:
1) changed the signature of View.findViewById(). Now I need to use findViewById<View>
2) getContext() of Fragment became not null safety so I need to add !! to all context values in all fragments.
Is there any way to fix it automatically for whole project? This is not a small project and do this fixes manually will take a lot of time.
There's not an easy way built in to migrate findViewById that I know of. I believe I recorded a Vim (IdeaVim) macro to help me when I did it.
The nullability of getContext() did not change, only the annotation did. In the past getContext() could have returned null, the compiler just didn't warn you. You should not try to find a way to ignore these errors, or even to add !! to each usage. You should instead handle the case where the return value is null and do the right thing in your application.
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.