Questions tagged [kotlin]

Kotlin is an open-source, statically typed programming language supported and developed by JetBrains. Kotlin combines OOP and functional features and is focused on interoperability, safety, clarity, and tooling support. It currently targets the JVM, JavaScript and native targets, and it's an officially supported language on Android.

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 May 5, 2021, The Kotlin team released version 1.5 (announcement / github release tag).

The current major version is 1.5.

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

57697 questions
306
votes
10 answers

Default interface methods are only supported starting with Android 7.0 (Nougat)

I upgraded to Android Studio 3.1 and I'm getting the following error: Default interface methods are only supported starting with Android N (--min-api 24): void…
j2emanue
  • 51,417
  • 46
  • 239
  • 380
296
votes
3 answers

How do I initialize Kotlin's MutableList to empty MutableList?

Seems so simple, but, how do I initialize Kotlin's MutableList to empty MutableList? I could hack it this way, but I'm sure there is something easier available: var pusta: List = emptyList() var cos: MutableList =…
ssuukk
  • 6,870
  • 6
  • 31
  • 43
295
votes
2 answers

Sort collection by multiple fields in Kotlin

Let's say I have a list of People which I need to sort by Age first and then by Name. Coming from a C#-background, I can easily achieve this in said language by using LINQ: var list=new List(); list.Add(new Person(25, "Tom")); list.Add(new…
Fire095
  • 3,035
  • 2
  • 8
  • 10
280
votes
5 answers

Kotlin and new ActivityTestRule : The @Rule must be public

I'm trying to make UI test for my android app in Kotlin. Since the new system using ActivityTestRule, I can't make it work: it compiles correctly, and at runtime, I get: java.lang.Exception: The @Rule 'mActivityRule' must be public. at…
Geob-o-matic
  • 5,203
  • 4
  • 29
  • 38
264
votes
13 answers

Android Get Current timestamp?

I want to get the current timestamp like that : 1320917972 int time = (int) (System.currentTimeMillis()); Timestamp tsTemp = new Timestamp(time); String ts = tsTemp.toString();
Rjaibi Mejdi
  • 6,162
  • 3
  • 18
  • 26
257
votes
9 answers

NullPointerException when trying to access views in a Kotlin fragment

How to use Kotlin Android Extensions with Fragments? If I use them inside onCreateView(), I get this NullPointerException exception: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View…
solidak
  • 4,693
  • 3
  • 25
  • 31
244
votes
8 answers

How to make primary key as autoincrement for Room Persistence lib

I am creating an Entity (Room Persistence Library) class Food, where I want to make foodId as autoincrement. @Entity class Food(var foodName: String, var foodDesc: String, var protein: Double, var carbs: Double, var fat: Double) { @PrimaryKey …
chandil03
  • 14,047
  • 5
  • 43
  • 64
238
votes
7 answers

Format in kotlin string templates

Kotlin has an excellent feature called string templates. I really love it. val i = 10 val s = "i = $i" // evaluates to "i = 10" But is it possible to have any formatting in the templates? For example, I would like to format Double in string…
MajesticRa
  • 12,389
  • 11
  • 54
  • 71
231
votes
9 answers

Extend data class in Kotlin

Data classes seem to be the replacement to the old-fashioned POJOs in Java. It is quite expectable that these classes would allow for inheritance, but I can see no convenient way to extend a data class. What I need is something like this: open data…
Dmitry
  • 3,012
  • 2
  • 12
  • 12
230
votes
8 answers

How to create empty constructor for data class in Kotlin Android

I have 10+ variables declared in Kotlin data class, and I would like to create an empty constructor for it like how we typically do in Java. Data class: data class Activity( var updated_on: String, var tags: List, var…
Sai
  • 12,738
  • 15
  • 70
  • 106
229
votes
10 answers

startForeground fail after upgrade to Android 8.1

After upgrading my phone to 8.1 Developer Preview my background service no longer starts up properly. In my long-running service I've implemented a startForeground method to start the ongoing notification which is called in on…
Rawa
  • 10,673
  • 6
  • 33
  • 52
229
votes
3 answers

Difference between List and Array types in Kotlin

What is the difference between List and Array types? It seems can make same operations with them (loops, filter expression, etc..), is there any difference in behavior or usage? val names1 = listOf("Joe","Ben","Thomas") val names2 =…
Daniel Hári
  • 5,551
  • 4
  • 36
  • 48
225
votes
13 answers

Constants in Kotlin -- what's a recommended way to create them?

How is it recommended to create constants in Kotlin? And what's the naming convention? I've not found that in the documentation. companion object { //1 val MY_CONST = "something" //2 const val MY_CONST = "something" //3 val…
Jodimoro
  • 2,875
  • 3
  • 8
  • 15
223
votes
3 answers

What is the Kotlin double-bang (!!) operator?

I'm converting Java to Kotlin with Android Studio. I get double bang after the instance variable. What is the double bang and more importantly where is this documented? mMap!!.addMarker(MarkerOptions().position(london).title("Marker in London"))
mbr_at_ml
  • 2,577
  • 2
  • 11
  • 12
219
votes
10 answers

Kotlin's List missing "add", "remove", Map missing "put", etc?

In Java we could do the following public class TempClass { List myList = null; void doSomething() { myList = new ArrayList<>(); myList.add(10); myList.remove(10); } } But if we rewrite it to Kotlin…
Elye
  • 30,821
  • 26
  • 115
  • 272