Questions tagged [kotlin]

Kotlin is a cross-platform, statically typed, general-purpose high-level programming language with type inference. This tag is often used alongside additional tags for the different targets (JVM, JavaScript, native, etc.) and libraries/frameworks (Android, Spring, etc.) used by Kotlin developers, if the question relates specifically to those topics.

Kotlin is an open-source, statically typed programming language supported and developed by JetBrains. It supports JVM bytecode, JavaScript, and Native code as compilation targets, and it has been an officially supported first-class language on Android since Google I/O 2017. The goals with Kotlin are to make it concise, safe, versatile, and have it be seamlessly interoperable with existing Java libraries.

On June 9th 2022, The Kotlin team released version 1.7.0 (announcement / github release tag).

The current major version is 1.7.

How to ask

If you are using Kotlin and your question is related to it, then you should add this tag. You should explain what do you intend to achieve, how did you try to achieve it, what the experienced behavior is and how is that different from your expectations.

Kotlin Reference Documentation

Kotlin Books

Development tools

Useful links

91039 questions
22
votes
3 answers

Multiple LiveData Observers After Popping Fragment

Issue Summary: Multiple LiveData Observers are being triggered in a Fragment after navigating to a new Fragment, popping the new Fragment, and returning to the original Fragment. Details: The architecture consists of MainActivity that hosts a…
22
votes
4 answers

Android Kotlin findViewById must not be null

We have created a custom alert dialog that was used in a Java project by converting it to Kotlin The error posted below java.lang.IllegalStateException: findViewById(R.id.btnYES) must not be null The error source is eluding us ! We have looked at a…
Vector
  • 3,066
  • 5
  • 27
  • 54
22
votes
2 answers

how to filter list base on another list in kotlin/java?

I have two types FooApi and FooModel: class FooApi (var aId) class FooModel(var mId) Is a way to simplify below function which filter FooModel list base on FooApi list: fun f(fooModelList: List, fooApiList: List) : List
LunaVulpo
  • 3,043
  • 5
  • 30
  • 58
22
votes
2 answers

In kotlin, how do I mock a suspend function that wraps a callback?

Let's say there's an interface with a callback: interface SomeInterface { fun doSomething(arg: String, callback: (Exception?, Long) -> Unit) } which I extend into a suspend function like this: suspend fun SomeInterface.doSomething(arg: String):…
Mircea Nistor
  • 3,145
  • 1
  • 26
  • 32
22
votes
2 answers

@Parcelize in multi-platform project

I'd like to create a class in a multi-platform project, and use Parcelize to make it Parcelable in the Android version. So in my lib-common project, I tried: expect annotation class Parcelize() expect interface Parcelable @Parcelize data class…
22
votes
2 answers

Kotlin data class and bean validation with container element constraints

With Bean Validation 2.0 it is possible to also put constraints on container elements. I cannot get this to work with Kotlin data classes: data class Some(val someMap: Map) This does not have any effect. Any…
Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
22
votes
7 answers

How to use companion objects on xml layout?

I am trying to use a companion object property inside the layout but the compiler doesn't recognise it. Kotlin Class class MyClass { companion object { val SomeProperty = "hey" } } XML Layout
melanke
  • 804
  • 1
  • 9
  • 25
22
votes
3 answers

How do I create a PagedList of an object for tests?

I have been working with the arch libraries from Google, but one thing that has made testing difficult is working with PagedList. For this example, I am using the repository pattern and returning details from either an API or network. So within…
22
votes
3 answers

How can I access a char in string in at specific number?

I tried this code but it is giving me errors. So how can I access a character in a string in kotlin? In java, it can be done by the charAt() method. private fun abc(x: String) { var i: Int = 0 while (x[i].toString() != "+") { var y:…
Aakash Sharma
  • 427
  • 1
  • 6
  • 15
22
votes
3 answers

Can we pass java method to to kotlin function of type () -> Unit?

I have a method of kotlin like below fun initDrawer(drawerView: DrawerView, drawerLayout: DrawerLayout, context: Context, onRefreshClick: () -> Unit) { } But when I try to pass initDrawer(drawerView,drawerlayout,this,onRefreshClick()) it gives…
avez raj
  • 2,055
  • 2
  • 22
  • 37
22
votes
7 answers

Kotlin: count occurrences of `charArray` in `String`

I have two strings val string1 = "Hello" val string2 = "Hello world" I have to count existence of each letter from string1 in string2 in Kotlin So far, I have written this much code and stuck with regex val string1_array = string1.toCharArray() val…
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
22
votes
2 answers

Union types in Java

I've been working with C# for a while and trying to get more familiar with Java. So I'm trying to migrate some of the basic patterns I use on daily basis in C# even only to understand the gap between JVM and dotnet and figure out how to deal with…
Michal Pawluk
  • 561
  • 1
  • 5
  • 11
22
votes
3 answers

java.lang.IllegalArgumentException: Parameter specified as non-null is null for Kotlin and WebView

I am trying to populate my WebView with custom HTML string and trying to show progress when it is not loaded, and hide it when finished. Here is my code: webView.settings.javaScriptEnabled = true webView.loadDataWithBaseURL(null,…
K.Os
  • 5,123
  • 8
  • 40
  • 95
22
votes
3 answers

Get rid of unnecessary root layouts for fullscreen activities

How to get rid of unnecessary root layouts for fullscreen activities? I have a simple fullscreen activity. The layout inspector shows a large hierarchy of root layouts provided by Android, that I don't need in fullscreen. Can I get rid of…
Blcknx
  • 1,921
  • 1
  • 12
  • 38
22
votes
4 answers

How to implement a java.util.function.Predicate as Kotlin lambda?

I need to pass a java.util.function.Predicate to a Java function. How can I implement it as Lambda in Kotlin? The Java-Function I need to call: public void foo(Predicate p) Java Lambda implemenation ✔ : foo(text-> true) Kotlin Lambda…
Chriss
  • 5,157
  • 7
  • 41
  • 75