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
3
votes
3 answers
Name conflicts when using Method extension and Inheritance
I have an extension
fun Fragment.showToast(message: String, toastLength: Int = Toast.LENGTH_SHORT) {
Toast.makeText(context, message, toastLength).show()
}
In project we're using MVP:
interface MyContract {
interface View {
fun…

Taras Parshenko
- 584
- 1
- 5
- 17
3
votes
2 answers
Why my data class giving null values unless i applied default on it?
I am trying to show data from the server using data class of kotlin. It's almost working fine but some case whenever I fetch a response I don't know why it is still giving null values unless I add a default value ("") for msg.
This is my data…

chari sharma
- 462
- 2
- 16
3
votes
2 answers
ParcelCreator lint suppression not working
I'm trying to add Parcelize implementation but it doesn't seem to be working.
This is whaat my project's gradle looks like:
buildscript {
ext.kotlin_version = '1.2.30'
repositories {
maven { url "https://jitpack.io" }
google()
jcenter()…

user8789149
- 121
- 4
- 12
3
votes
1 answer
Kotlin pass array of custom objects via Intents and receive as a ArrayList
I have a couple of activities and I pass data between them using Intents. I want to pass an array of my custom object from the first activity and make it an arraylist in the second activity. The code that I have is:
data class Attachment(val Name:…

Sankar
- 6,192
- 12
- 65
- 89
3
votes
1 answer
kotlin connect to self-signed https server
I have the following kotlin code:
val urlPath = "https://10.0.2.2:8080"
var data: String
try {
data = URL(urlPath).readText()
} catch (e: Exception) {
Log.e("doInBackground", "Exception caught: ${e.localizedMessage}")
error = when (e) {
…

Sankar
- 6,192
- 12
- 65
- 89
3
votes
3 answers
Kotlin extension function - How to make it global?
I have created a Kotlin extension function on the Android ViewGroup class so that it can do layout inflation calls easier. So, here is my extension:
fun ViewGroup.inflate(layoutRes: Int): View {
return…

j2emanue
- 60,549
- 65
- 286
- 456
3
votes
1 answer
Kotlin classes without curly braces
I have noticed that we can create classes in Kotlin without curly braces like below.
//Example classFile.kt
class Empty
class SecondEmpty
fun firstMethod() {
}
My question is, why we need such feature? in which situation we can use this?
In the…

Zumry Mohamed
- 9,318
- 5
- 46
- 51
3
votes
1 answer
Kotlin: When extension function hides the class default implementation?
I'm trying to figure out when overriding an existing class function, with an extension function of the same signature - will take effect?
Here is my sample code:
fun String.toUpperCase(): String = "ext. function impl."
fun main(args: Array)…

Lior Bar-On
- 10,784
- 5
- 34
- 46
3
votes
3 answers
android studio plugin with id: 'kotlin-android-extensions'
I am using gradle and I am trying to add kotlin to my project. But when I am trying to add 'kotlin-android-extensions' plugin for gradle it fails to find it.

Ziad Saad
- 43
- 2
- 6
3
votes
1 answer
How to fix signature of generic extension method in kotlin to resolve "Type inference failed" in kotlin
I have created extension method:
@Suppress("UNCHECKED_CAST")
operator fun View.get(@IdRes id:Int): T =
this.findViewById(id) as T
Main usage of this method:
class A {
lateinit var text: TextView
fun init(view:View) {
…

curioushikhov
- 2,461
- 2
- 30
- 44
3
votes
1 answer
Setting a kotlin extension
I am unsure if it is possible for a kotlin extension to be set like a java object.
In my program I have a java class called Submission and I wanted to create a kotlin extension of that called categories - an ArrayList - so I made it like this.
var…

nmu
- 1,442
- 2
- 20
- 40
3
votes
2 answers
Kotlin extension function on mutable property
I'm trying to set an extension function on a mutable property so I can reassign the property in the extension function. I wanted to know if it was possible.
My goals is to make Date extensions for easy access. For example:
fun Date.addDays(nrOfDays:…

Kevin van Mierlo
- 9,554
- 5
- 44
- 76
3
votes
1 answer
Kotlin- Extension functions and platform types?
I want to add two extension functions to ResultSet that gets a value as a LocalDate.
fun ResultSet.getLocalDate(colName: String) = getDate(colName)?.toLocalDate()
fun ResultSet.getLocalDate(colIndex: Int) = getDate(colIndex)?.toLocalDate()
The…

tmn
- 11,121
- 15
- 56
- 112
3
votes
1 answer
Android library can not compile kotlin
My project has two projects: main project and library project.
The main project dependency library project.
Now I want to use kotlin both of them. I had added my common rx extension code into library project:
fun String.rxRequest(@NotNull builder:…

WangJie
- 526
- 3
- 7
2
votes
0 answers
How to access views in classes using viewbinding?
I have a XML file and I want to use a text view from inside that, to the best of my knowledge, view binding can be used to access views from id using:
binding =…

Ayush Tiwari
- 91
- 1
- 8