Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions.
Questions tagged [kotlin-extension]
333 questions
7
votes
1 answer
Is it possible to add an operator-overloading extension function in Kotlin?
I mean something like this:
fun operator Table.get(column_name: String) = this.column(column_name)
// Currently gives an error: "Expecting a top level declaration"
Table instance currently works like: table.column("column_name")
I want to make it…

Veneet Reddy
- 2,707
- 1
- 24
- 40
7
votes
1 answer
Kotlin annotate receiver of extension function
I would like to restrict on which constant value extension function can be called. For example function like:
@IdRes
fun Int.find() = findViewById(this)
If this was called on real id, it's fine:
R.id.someView.find() //…

xinaiz
- 7,744
- 6
- 34
- 78
7
votes
4 answers
Gradle Sync Issue Kotlin after Android 3.1 Stable Update
Hey after updating Android Studio to 3.1 Stable version i am getting following error to all my projects. Any help will be appreciated.
Could not download kotlin-reflect.jar (org.jetbrains.kotlin:kotlin-reflect:1.1.3-2): No cached version available…

Joker
- 537
- 1
- 6
- 19
7
votes
1 answer
How to access a view from layout specified in headerLayout of NavigationView using Kotlin in Android
I want to access a TextView which is included inside headerLayout of NavigationView. Is it possible to access the view using Kotlin android extension? I did using this method, but the TextView (here txtName) is always null.
Here is my…

Sandra
- 73
- 4
7
votes
5 answers
Can't access EditText or other UI components with Kotlin
I'm using Android Studio 3.0 RC2 & Kotlin.
When I try to access a UI component the app crashes unless I first write findViewById. I thought Kotlin was supposed to get rid of having to write findViewById line? The UI is a fragment and I'm trying to…

user1184205
- 863
- 1
- 10
- 23
6
votes
1 answer
Kotlinx synthetic and AndroidX
After migrating to AndroidX I found that in some, not all of my classes the kotlinx.android.synthetic fields are now failing to convert to the actual class.
Widget has an unresolved type 'androidx.core.widget.DrawerLayout', and
thus it was…

Anthony
- 7,638
- 3
- 38
- 71
6
votes
1 answer
java.lang.NoClassDefFoundError when using Kotlin AAR in Android Java application
I have to make a library(aar) in Kotlin and have to use that in Android application made using Java. Kotlin library has a class with companion functions to use statically in Java application. When I use that static method in my Android application…

MaheshGupta
- 143
- 3
- 12
6
votes
1 answer
Kotlin's @Parcelize throws NPE on writeToParcel()
I have this set of data classes, all in the same file:
@Parcelize data class Response(val RecordView: RecordView):Parcelable
@Parcelize data class RecordView(val Records: List):Parcelable
@Parcelize data class Gid(val Value:…

TooManyEduardos
- 4,206
- 7
- 35
- 66
6
votes
2 answers
Why do we have functions that named componentN in Kotlin
I've just looked at Kotlin standard library and found some strange extension-functions called componentN where N is index from 1 to 5.
There are functions for all types of primitives. For example:
/**
* Returns 1st *element* from the…

icarumbas
- 1,777
- 3
- 17
- 30
6
votes
1 answer
Kotlin get type of generic class without instance
I want go get Type of T, but I can not get it from instance. I have to get it from class parameter, how to do it?
abstract class ViewModelFragment{
protected lateinit var mViewModel: T
override fun onViewCreated(view: View,…

murt
- 3,790
- 4
- 37
- 48
6
votes
4 answers
Boolean extension function
When I try to create Extension Function to set Boolean true or false like the below.
Boolean.setTrue(){
this = true
}
Boolean.setFalse(){
this = false
}
It says variable expected. How to achieve this.

Bhuvanesh BS
- 13,474
- 12
- 40
- 66
6
votes
2 answers
Can I send extension function through function parameter
I have some extension functions function below.
fun EditText.setEmailValidationListener(): TextWatcher {
val textWatcher = object : TextWatcher {
override fun beforeTextChanged(text: CharSequence?, start: Int, count: Int, after: Int) {…

Elye
- 53,639
- 54
- 212
- 474
6
votes
2 answers
In kotlin, how to return an instance defined by generic class parameter
I'm trying to write a nice Kotlin wrapper for a web framework against kotlin 1.0.3. In that I am trying to mixin a function to the request to have it return a bean via a JSON transformation using jackson.
So in my library I have the…

Ryba
- 1,249
- 10
- 17
6
votes
2 answers
Kotlin extension functions by type
What is the idiomatic way to add a function to a class based on type. The following example uses List as the class, and the Type Parameter is the class of objects inside of the list. Lets say you want to sort each of these lists with a different…

Jeremy Lyman
- 3,084
- 1
- 27
- 35
5
votes
1 answer
what is the difference between lazy and lazyFast in kotlin?
what is the difference between Kotlin's lazy delegate property lazy and lazyFast?.because it looks like same code.
private val key: String by lazy {
if (arguments != null && arguments?.getString(Constants.KEY) != null) {
…

Ronak Ukani
- 603
- 5
- 20