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
945
votes
25 answers

Place cursor at the end of text in EditText

I am changing the value of an EditText on keyListener. But when I change the text the cursor is moving to the beginning of the EditText. I need the cursor to be at the end of the text. How to move the cursor to the end of the text in a EditText.
Manu
  • 9,459
  • 3
  • 12
  • 3
828
votes
20 answers

Unfortunately MyApp has stopped. How can I solve this?

I am developing an application, and everytime I run it, I get the message: Unfortunately, MyApp has stopped. What can I do to solve this? About this question - obviously inspired by What is a stack trace, and how can I use it to debug my…
nhaarman
  • 90,392
  • 51
  • 233
  • 265
761
votes
29 answers

What is the equivalent of Java static methods in Kotlin?

There is no static keyword in Kotlin. What is the best way to represent a static Java method in Kotlin?
pdeva
  • 36,445
  • 42
  • 122
  • 154
714
votes
32 answers

Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6

When trying to run the Example CorDapp (GitHub CorDapp) via IntelliJ, I receive the following error: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6 How can I modify the IntelliJ settings so…
Joel
  • 24,072
  • 6
  • 20
  • 37
593
votes
32 answers

Kotlin Ternary Conditional Operator

What is the equivalent of this expression in Kotlin? a ? b : c This is not valid code in Kotlin.
Drew Noakes
  • 266,361
  • 143
  • 616
  • 705
586
votes
9 answers

How to check if a "lateinit" variable has been initialized?

I wonder if there is a way to check if a lateinit variable has been initialized. For example: class Foo() { private lateinit var myFile: File fun bar(path: String?) { path?.let { myFile = File(it) } } fun bar2() { …
Mathew Hany
  • 9,598
  • 4
  • 16
  • 15
540
votes
13 answers

How to make an Android device vibrate? with different frequency?

I wrote an Android application. Now, I want to make the device vibrate when a certain action occurs. How can I do this?
Billie
  • 8,100
  • 12
  • 31
  • 62
505
votes
14 answers

How to launch an Activity from another Application in Android

I want to launch an installed package from my Android application. I assume that it is possible using intents, but I didn't find a way of doing it. Is there a link, where to find the information?
Bastian
  • 8,555
  • 6
  • 17
  • 8
457
votes
9 answers

How to convert a Kotlin source file to a Java source file

I have a Kotlin source file, but I want to translate it to Java. How can I convert Kotlin to Java source?
activedecay
  • 8,238
  • 3
  • 40
  • 54
361
votes
11 answers

Alarm Manager Example

I want to implement a schedule function in my project. So I Googled for an Alarm manager program but I can`t find any examples. Can anyone help me with a basic alarm manager program?
Rajamohan Sugumaran
  • 4,129
  • 4
  • 20
  • 19
354
votes
9 answers

Kotlin - Property initialization using "by lazy" vs. "lateinit"

In Kotlin if you don't want to initialize a class property inside the constructor or in the top of the class body, you have basically these two options (from the language reference): Lazy Initialization lazy() is a function that takes a lambda…
regmoraes
  • 4,027
  • 2
  • 19
  • 31
341
votes
8 answers

What is the difference between "const" and "val"?

I have recently read about the const keyword, and I'm so confused! I can't find any difference between const and the val keyword, I mean we can use both of them to make an immutable variable, is there anything else that I'm missing?
Mathew Hany
  • 9,598
  • 4
  • 16
  • 15
327
votes
10 answers

Smart cast to 'Type' is impossible, because 'variable' is a mutable property that could have been changed by this time

And the Kotlin newbie asks, "why won't the following code compile?": var left: Node? = null fun show() { if (left != null) { queue.add(left) // ERROR HERE } } Smart cast to 'Node' is impossible, because 'left' is a…
FRR
  • 10,772
  • 13
  • 43
  • 80
323
votes
37 answers

What is the difference between var and val in Kotlin?

What is the difference between var and val in Kotlin? I have gone through this link: KotlinLang: Properties and Fields As stated on this link: The full syntax of a read-only property declaration differs from a mutable one in two ways: it starts…
Akshar Patel
  • 7,525
  • 5
  • 31
  • 44
310
votes
20 answers

How to initialize an array in Kotlin with values?

In Java an array can be initialized such as: int numbers[] = new int[] {10, 20, 30, 40, 50} How does Kotlin's array initialization look like?
Lars Blumberg
  • 14,526
  • 9
  • 73
  • 107
1
2 3
99 100