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
1058
votes
27 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
  • 10,589
  • 3
  • 15
  • 3
1055
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
  • 43,605
  • 46
  • 133
  • 171
877
votes
23 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
  • 98,571
  • 55
  • 246
  • 278
836
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
  • 13,310
  • 4
  • 19
  • 16
822
votes
34 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
  • 300,895
  • 165
  • 679
  • 742
775
votes
37 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
  • 22,762
  • 5
  • 26
  • 41
602
votes
14 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,938
  • 12
  • 37
  • 67
557
votes
18 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
  • 9,505
  • 6
  • 19
  • 8
526
votes
8 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
  • 10,129
  • 5
  • 47
  • 71
477
votes
9 answers

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
  • 5,329
  • 3
  • 24
  • 33
454
votes
9 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
  • 13,310
  • 4
  • 19
  • 16
412
votes
13 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…
frankelot
  • 13,666
  • 16
  • 54
  • 89
399
votes
27 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
  • 19,326
  • 11
  • 90
  • 127
392
votes
10 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,439
  • 4
  • 22
  • 19
388
votes
3 answers

How does the reified keyword in Kotlin work?

I'm trying to understand the purpose of the reified keyword. Apparently, it's allowing us to do reflection on generics. However, when I leave it out, it works just as fine. When does this make an actual difference?
hl3mukkel
  • 5,369
  • 3
  • 21
  • 21
1
2 3
99 100