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
166
votes
12 answers

How to get Context in Jetpack Compose

fun createListItem(itemIndex: Int) { Padding(left = 8.dp, right = 8.dp, top = 8.dp, bottom = 8.dp) { FlexRow(crossAxisAlignment = CrossAxisAlignment.Center) { expanded(1.0f) { Text("Item $itemIndex") …
166
votes
7 answers

when to use an inline function in Kotlin?

I know that an inline function will maybe improve performance & cause the generated code to grow, but I'm not sure when it is correct to use one. lock(l) { foo() } Instead of creating a function object for the parameter and generating a call, the…
holi-java
  • 29,655
  • 7
  • 72
  • 83
166
votes
4 answers

Android Room - Select query with LIKE

I'm trying to make a query to search all objects whose names contain text: @Query("SELECT * FROM hamster WHERE name LIKE %:arg0%") fun loadHamsters(search: String?): Flowable> Messages: Error:no viable alternative at input 'SELECT *…
Denis Buzmakov
  • 1,662
  • 2
  • 10
  • 8
164
votes
4 answers

Difference between ArrayList() and mutableListOf() in Kotlin

private val repositories = mutableListOf() private val repositories = ArrayList() Both are mutable list, then what is the point of two keywords mutableListOf or ArrayList? or is there any major difference?
Sai
  • 15,188
  • 20
  • 81
  • 121
163
votes
5 answers

How to catch many exceptions at the same time in Kotlin?

try { } catch (ex: MyException1, MyException2 ) { logger.warn("", ex) } or try { } catch (ex: MyException1 | MyException2 ) { logger.warn("", ex) } As a result, a compilation error: Unresolved reference: MyException2. How can I catch…
Ant20
  • 1,993
  • 2
  • 12
  • 16
163
votes
13 answers

Kotlin secondary constructor

How do I declare a secondary constructor in Kotlin? Is there any documentation about that? Following does not compile... class C(a : Int) { // Secondary constructor this(s : String) : this(s.length) { ... } }
ironic
  • 8,368
  • 7
  • 35
  • 44
161
votes
29 answers

ListAdapter not updating item in RecyclerView

I'm using the new support library ListAdapter. Here's my code for the adapter class ArtistsAdapter : ListAdapter(ArtistsDiff()) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { …
Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
160
votes
10 answers

RegisterResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)

Using new android studio with new 3.0.0 Gradle pluging. When building some warning happened: registerResGeneratingTask is deprecated, use registerGeneratedFolders(FileCollection)
Valentin Baryshev
  • 2,195
  • 3
  • 15
  • 24
160
votes
9 answers

How to use @link and @code in kotlin kDoc

I'm trying to document a method and trying to use @link and @code as in JavaDoc. I know in kotlin there is a kDoc but I can't find them or at least something similar.
humazed
  • 74,687
  • 32
  • 99
  • 138
160
votes
2 answers

How to create an instance of anonymous class of abstract class in Kotlin?

Assume that KeyAdapter is an abstract class with several methods that can be overridden. In java I can do: KeyListener keyListener = new KeyAdapter() { @Override public void keyPressed(KeyEvent keyEvent) { // ... } }; How to do the…
Tvaroh
  • 6,645
  • 4
  • 51
  • 55
158
votes
5 answers

Kotlin asterisk operator before variable name or Spread Operator in Kotlin

I want to know what exactly an asterisk does before a variable name in Kotlin. I saw this (*args) in a Spring boot Kotlin example: @SpringBootApplication open class Application { @Bean open fun init(repository: CustomerRepository) =…
mojtab23
  • 2,495
  • 5
  • 22
  • 31
158
votes
6 answers

Best way to null check in Kotlin?

Should I use double =, or triple =? if(a === null) { //do something } or if(a == null) { //do something } Similarly for 'not equals': if(a !== null) { //do something } or if(a != null) { //do something }
pdeva
  • 43,605
  • 46
  • 133
  • 171
156
votes
21 answers

Kotlin Error : Could not find org.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7

I installed the Kotlin plugin into my app (v. v1.1.1-release-Studio2.2-1) and then selected "Configure Kotlin in Project" I selected compiler and runtime version of 1.0.7. Kotlin updated my Gradle files. Now when I try to build in I get: Error: A…
Mike6679
  • 5,547
  • 19
  • 63
  • 108
156
votes
12 answers

Opening Android Settings programmatically

How can I open settings programmatically?
Behnam
  • 6,510
  • 6
  • 35
  • 65
153
votes
3 answers

Kotlin - Void vs. Unit vs. Nothing

Kotlin has three types that are very similar in nature: Void Unit Nothing It almost seems like they're making the JavaScript mistake: null undefined void(0) Assuming that they haven't fallen into the same mistake, what are they all for, and how…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313