336 questions
0
votes
1
answer
115
views
Using Moshi in an extension function or injecting it via DI in Android
In Android, I am using an extension function to map a failed network response to my sealed ErrorResponse class.
@JsonClass(generateAdapter = true)
data class ErrorResponse(
@Json(name = "code&...
0
votes
0
answers
31
views
How can I restrict generic parameter to be non-nullable? [duplicate]
I have following signature:
fun <T, R> JsonNullable<T>.map(func: (T) -> R): JsonNullable<R> {
How can I restrict T and R to be non-nullable ?
0
votes
0
answers
54
views
What is the puripose of `additional receiver type` in Kotlin lambda declaration?
I am a java developer but develop Kotlin for two weeks and during test implementation I've found interesting construction :
every {
mockedObject.foo(arg)
} returns bar
I've checked every finction ...
7
votes
1
answer
829
views
Can Kotlin extension functions be called without an import declaration?
Is it possible to call an extension function from another package without importing it?
Given an extension function:
package ext
fun Int.plusOne() = this + 1
Is there any way to call this function ...
1
vote
1
answer
822
views
Kotlin. How to reset each array element to zero?
I'm using FloatArray:
private val values = FloatArray(5)
At some point, I need to reset each array value to zero. I tried doing it like this:
values.onEachIndexed { index, value -> value[index]...
-1
votes
3
answers
127
views
how to add text in top border of view in andriod
I am new to android development please help me in creating this type design
I want to add text in the center of the top borders in the card
please help me or guide me to achive the same view
0
votes
0
answers
76
views
Kotlin extensions method
How can I write extension function in kotlin for such function:
public abstract fun <T : TargetValue> getAverage(metric: StatisticsMetric,
interval: DateInterval,
timeUnit: TimeUnit,
mode: ...
3
votes
4
answers
7k
views
Kotlin. What is the best way to replace element in immutable list?
What is the best way to update specific item in immutable list. For example I have list of Item. And I have several ways to update list:
1.
fun List<Item>.getList(newItem: Item): List<Item&...
1
vote
1
answer
785
views
How to shadow member function with a custom Kotlin extension function? (like Kotlin stdlib does)
I am reading Kotlin in Action 2nd edition.
Chapter 3 says:
If the class has a member function with the same signature as an extension function, the member function always takes precedence
At the ...
0
votes
2
answers
461
views
Kotlin. How to set type of generic in runtime?
Is it possible to set type of filterIsInstance extension at runtime.
For example I have list of Item and I nee to cast my list by specific condition:
fun (items: List<Item>) {
items....
0
votes
1
answer
447
views
How to use same viewModel method inside multiple fragment?
I want use one viewModel and observe object from one method. But I dont want write this observe method in all fragment. Only write in one place and use other fragments. I think I need fragment ...
3
votes
2
answers
223
views
Casting Integer to Unit compiles successfully
Why does this program prints kotlin.Unit when it should fail with ClassCastException at runtime?
class Animal<T> {
}
fun <T> Animal<T>.extension(block: ()-> T){
print(...
-1
votes
2
answers
760
views
Generic extension function Kotlin
Since getParcelable is deprecated for Android Tiramisu and later i want to make an extension function to get the parcelables from my Intent through my Bundle
So i have
val server = if (...
0
votes
1
answer
752
views
How to implement KTX updateLayoutParams() extension
How do I add the KTX updateLayoutParams() extension to my project?
When I use it, Android Studio says "Unresolved reference: updateLayoutParams" even though I added this to gradle:
...
1
vote
2
answers
757
views
How can I call Kotlin extension function with receiver from the Java static method?
Probably I miss something very simple, but let's imagine I have this extension method in Kotlin:
fun Any.log(message: String) {
println(this.javaClass.simpleName + message)
}
Is it possible to ...
1
vote
1
answer
575
views
Android Formatting String in Extension Function
I am trying to get Formatting String in AndroidViewModel by using following extension function but failed:
<string name="version_text">Version %1$s</string>
val versionText = ...
2
votes
0
answers
563
views
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 = ActivityMainBinding.inflate(...
1
vote
2
answers
629
views
kotlin-android-extensions not working. what will be the problem?
I was following some guide(download android studio today) of kotlin and I have use the setText and it's not working.
what will be the problem?
package com.example.basic
import androidx.appcompat.app....
0
votes
1
answer
813
views
How to create extension function to display progress bar in Kotlin?
I am learning to create extension functions in Kotlin and get success in some. I want to create 2 extension functions 1 for display Progress bar when and where ever I call that function and 2 is for ...
3
votes
1
answer
741
views
Kotlin, implement extension var property
It seems that a simple extension property like the following does not work.
var Dog.age = 0;
What is the recommended way to implement this? I have tried the following, and it worked, but this will ...
1
vote
1
answer
1k
views
How to pass a parameter to a extension function in Kotlin
I have an extension function in kotlin to check is it a valid string or not as stated below.
fun EditText.onAfterTextChanged(listener: (String) -> Unit) {
addTextChangedListener(object : ...
0
votes
1
answer
1k
views
how to create tryCatch extension function
i like to achieve this kind of code:
someKindOfCode.tryCatch()
i kind of created it but Any doesn't do the job and don't put the code inside of tryCatch
private fun Any.tryCatch() {
try {
...
0
votes
1
answer
197
views
Issue with custom dialog after removing Kotlin extension from the Android project
I have a custom dialog in my android project and it was working fine. After I removed kotlin extension from the project, I have modified my code as follows but there is some issue with the Views in ...
2
votes
1
answer
3k
views
In Kotlin, how do I convert "CompletableFuture<Optional<T>>" to "Flow<T?>"?
I'm trying to convert a CompletableFuture<Optional<T>> to a Flow<T?>. The extension function I'm trying to write is
fun <T> CompletableFuture<Optional<T>>....
0
votes
1
answer
328
views
Intent Activity Utils , not declarated in Manifest - Kotlin, Android
I am trying to achieve Intent Activity Utils as extended functions in Kotlin. They will start activities from other Activity/Fragment. Please suggest to me the best way to do that().
I tried to solve ...