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
0
votes
1 answer
Kotlin Extension Function On a Generic Interface
I am trying to write an extension function for a Kotlin interface that simply returns an Observable of the object. So far, this is what I have been able to do, but I don't know if it is the best way to implement this functionality.
Here is what I…

anthonylawson
- 761
- 9
- 24
0
votes
0 answers
Android lint shows UnusedIds after converting findViewById to kotlin synthetic
I converted findViewById to kotlin synthetic after I did that I get the below error. But everything works the same. I have the exact issue before in fragment. In there I solve it by accessing the id after the onCreateView() i.e onViewCreated(). here…

gouri panda
- 312
- 2
- 11
0
votes
1 answer
Accessing outer this in extension property
I have function like this that works fine:
private fun List.toStringArray() = list().apply {
for (hasName in this@toStringArray) add(hasName.name)
}.toTypedArray()
I wanted to convert it to property syntax like…

Renetik
- 5,887
- 1
- 47
- 66
0
votes
0 answers
How it's possible to extend function type in kotlin?
I just can't understand why it is possible to extend function type in kotlin ? And then why we can't extend String? then, which is also a type.
interface CustomFunctionType : () -> Unit {
// this is possible, buy how?
}

abhi
- 961
- 9
- 13
0
votes
1 answer
Extending Kotlin's extension
I'm trying to create an extension for KLogger/Logger which is an extension for slf4j's logger in order to better handle structured log messages. Could someone explain what's wrong with my code and how to fix it or work around it without passing my…

Robert Gonciarz
- 363
- 2
- 8
- 15
0
votes
1 answer
Can't use kotlin-extension in Adapter
Android Studio 3.6
build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
android {
dataBinding {
enabled = true
}
…

Alexei
- 14,350
- 37
- 121
- 240
0
votes
4 answers
How to check if Image Quality is Low
I am developing a PhotoBook app where I need to notify the user if they add a low-resolution image. I just need to show a "Low-Resolution Image, Printing may be affected" warning as chatbooks app do.

Hitesh Kushwah
- 1,351
- 13
- 23
0
votes
1 answer
Kotlin: How to access a field with the same name as an extension?
I have the following field extension
import com.google.gson.annotations.SerializedName
val Enum<*>.serializedName: String get() = javaClass.getField(name).getAnnotation(SerializedName::class.java).value
Now if my enum has the same field name as…

Quang Ngô
- 45
- 1
- 5
0
votes
1 answer
Extension function with different signature is confusing with different extension function in kotlin
class Example
fun main(){
val example: Example = Example()
// what function i am calling is it fun Example.extensionFun() ?
// Or is it Example.extensionFun(string: String) ?
example.extensionFun()
}
// first extension…
user10614828
0
votes
2 answers
Is there any way I can get context from fragmentmanager?
I have an extension function with FragmentManager as receiver. I have used it at atleast 75 places. Now I want to access sharedpreference in that extension. All I need is context inside that function. Any way I can access context?
Here is how…

Aziz
- 1,976
- 20
- 23
0
votes
1 answer
Using Kotlin's extension function to suppress a returned value
I am using Kotlin in a spring boot application. Especially in the services, I have found that some of the function need to suppress the returned value from repository. For example, here is a save() that saves an entity without returning the…

Prashant
- 4,775
- 3
- 28
- 47
0
votes
1 answer
Can I make nice Kotlin extension function?
Here is my code
theme?.let {
....
titleText.setTextColor(it.pTextColor)
contentText.setTextColor(it.pTextColor)
}
}
and the usage of this extension is here
cardView.setTheme(theme)
How can I have this cardView.theme = theme

Artur A
- 257
- 3
- 20
0
votes
3 answers
What is the alternative for utils in Kotlin?
I have this piece of code that repeats in 3 places
if (orientation == AppConstants.LANDSCAPE) {
width =
height =
}else{
width =
height =
}
imageUrl.let {
val options =…

Artur A
- 257
- 3
- 20
0
votes
1 answer
How to make this code nicer should I have utils method or extension?
So this getLocalizedTitle() function I have for this 3 type of classes, should I have as utils and pass titles to this function or there can be nicer solution with extensions?
data class Carousel(val id: String, val titles: List) :…

Artur A
- 257
- 3
- 20
0
votes
1 answer
Extension property: Function declaration must have a name Unresolved reference: get
I use extension function to extend retrofit2.Response object:
Snippet:
public class ErrorResponse {
private int code;
private String message;
private Response response;
}
import okhttp3.MediaType
import okhttp3.Protocol
import…

Alexei
- 14,350
- 37
- 121
- 240