Questions tagged [kotlin-extension]

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.

333 questions
13
votes
3 answers

How to use ViewModelProviders in Kotlin

I am new bie to Kotlin, Please help me how to use ViewModelProviders.of(this) in Kotlin My code in java is mFavViewModel = ViewModelProviders.of(this).get(FavouritesViewModel.class); I am not able to find ViewModelProviders class in kotlin tried…
Uma Achanta
  • 3,669
  • 4
  • 22
  • 49
13
votes
6 answers

Kotlin extension function start activity with Intent extras

I am trying to write a kotlin extension function for Context which will start a new activity in android with given class name and list of intent extras. I am able to successfully start activity without any extras but I am facing problem with…
13
votes
2 answers

Android KTX or Anko

I'm a little confused about the advantages and disadvantages of using Android KTX vs Anko. Because both libraries are trying to achieve the same end goal, and the line between them it's getting a little bit blurry to the point, in some cases, the…
Arturo Mejia
  • 1,862
  • 1
  • 17
  • 25
13
votes
4 answers

kotlin: extension methods and null receiver

In lombok extension method obj.method() is a syntax sugar for SomeUtil.method(obj). It allows for obj be null. Kotlin extensions methods are resolved statically so I assume it's the same syntactic sugar. But when I wrote fun Any.stringOrNull() =…
piotrek
  • 13,982
  • 13
  • 79
  • 165
13
votes
1 answer

Kotlin inlined extension property

I know inline keyword means to avoid the call overhead calling a funtion. But I can't figure out what inline a extension property work for? Let say we have two extension property named foo and another with is inlined named bar val Any.foo : Long …
crgarridos
  • 8,758
  • 3
  • 49
  • 61
13
votes
2 answers

Cannot resolve string supplied to vararg parameter in extension function

strings.xml Showing your number: %1$s ActivityExt.kt fun Activity.showToast(textResId: Int, vararg formatArgs: String) { val text = getString(textResId, formatArgs) Toast.makeText(this, text,…
Yuriy Seredyuk
  • 1,643
  • 2
  • 15
  • 18
13
votes
1 answer

Protected members not accessible in extension functions?

Kotlin features a couple of visibility modifiers as well as extension functions. The documentation states that Extensions are resolved statically. But what does this mean for the visibility of class members within extension functions? Let's consider…
André Diermann
  • 2,725
  • 23
  • 28
12
votes
3 answers

Unresolved reference: async in Kotlin in 1.3

I have multi module kotlin gradle project in github here. One of my sub project introducing-coroutines with build file build.gradle.kts file is here The contents of build.gradle.kts is - import org.jetbrains.kotlin.gradle.dsl.Coroutines …
user51
  • 8,843
  • 21
  • 79
  • 158
12
votes
3 answers

Android Studio and Kotlin: Unresolved reference: also

also compiles and runs from Android Studio: val greeted = "World".also { println("Hello $it") } Yet the editor highlights also and it as errors and don't give any help with intellisense. apply still works and when I go to source I can see the…
Love
  • 1,709
  • 2
  • 22
  • 30
11
votes
4 answers

Where to declare Kotlin extension functions in an Android app

Suppose I have the following code that I want to make as a re-usable component: fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] =…
Johann
  • 27,536
  • 39
  • 165
  • 279
11
votes
3 answers

Testing extension functions inside classes

If we want to test an extension function on a type, we can create an instance of this type, call the function and check the returned value. But what about testing extension functions defined inside classes? abstract class AbstractClass { fun…
JavierSA
  • 783
  • 1
  • 10
  • 25
11
votes
2 answers

Kotlin extension method as alias for long method name?

I am working in Kotlin using a Kotlin-native library object containing a method whose .nameIsMuchTooLongAndIsStillNotClear. In a manner similar to typealias, I want to create an alias to the method, so I can refer to it as something .shortAndClear.…
sirksel
  • 747
  • 6
  • 19
11
votes
2 answers

How to create Kotlin DSL - DSL syntax Kotlin

As with anko you can write callback functions like this: alert { title = "" message = "" yesButton { toast("Yes") } noButton { toast("No") } } How can I create a nested functions like that? I tried creating…
kirtan403
  • 7,293
  • 6
  • 54
  • 97
11
votes
8 answers

How to create a button in Kotlin that opens a new activity (Android Studio)?

Hello I'm making an app using Android Studio and the Kotlin language and am having trouble getting my button to open a new activity. I have the button created in my xml file but I can't find the KOTLIN syntax of how to declare it in MainActivity.kt…
10
votes
1 answer

Error: Kotlin: The floating-point literal does not conform to the expected type Float

I was making a simple maths calculator in kotlin, an error appeared on my screen when I tried to initialize the value of one of the variables used as 0.00 for float integer. var x:Float= readLine()!!.toFloat() var y:Float= readLine()!!.toFloat() var…
plethorax
  • 149
  • 1
  • 1
  • 8
1 2
3
22 23