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
14
votes
2 answers

Why can "return" return a "return" in Kotlin?

The question may sound silly, but there is no typo in it. fun test(): Any { return return true } This is actually possible in Kotlin. Although the compiler warns about Unreachable code for the outer return. But this is just a warning. I don't…
Willi Mentzel
  • 21,499
  • 16
  • 88
  • 101
14
votes
4 answers

How to destruct a Kotlin list into sublists?

How can I destruct a list in Kotlin into two sublists? Currently I do something like this: val (first, rest) = listOf("one", "two", "three") But doing so, first is "one" and rest is "two". I want them to be first =["first"] and rest = ["two",…
Ueli Hofstetter
  • 2,109
  • 2
  • 21
  • 48
14
votes
5 answers

Simple and nice way to increment nullable Int in Kotlin

What is the simplest and best readable way to increment nullable Int in Kotlin? Is there any other way than doing this? var myInt: Int? = 3 myInt = if(myInt!=null) myInt+1 else null This is quite fine if myInt is simple variable, but it can grow…
Aleš Zrak
  • 361
  • 1
  • 3
  • 14
14
votes
4 answers

Kotlin showed type mismatch in fragment

I have a fragment with RecyclerAdapter inside it. I want to initialize the adapter in the onCreateView method but it throws the error of "Type mismatch. Required : Context , Found : FragmentActivity" in this statement I have no idea why the first…
Long Ranger
  • 4,766
  • 7
  • 38
  • 65
14
votes
4 answers

Java/Android/Kotlin: Reflection on private Field and call public methods on it

Is it possiable to use reflection to access a object's private field and call a public methods on this field? i.e class Hello { private World word } class World { public BlaBlaBla foo() } Hello h = new Hello() World world = reflect on the…
TeeTracker
  • 5,958
  • 4
  • 33
  • 42
14
votes
2 answers

how to create an array of repeating object in kotlin?

I know how to do it by creating a loop but I wanted to know if there's an easier way? for example, I want to create an array of Point and they will all have (0,0) or increment x,y by their index.
user1865027
  • 2,915
  • 4
  • 26
  • 56
14
votes
2 answers

What are advances of glide recyclerview integration?

I just tried to use glide recyclerview integration and read a document about it and it said: "The RecyclerView integration library makes the RecyclerViewPreloader available in your application. RecyclerViewPreloader can automatically load images…
14
votes
2 answers

Why Room entities don't work with immutable properties in Android

I have been exploring Room database object mapping library and I figured something weird. An entity data model cannot have immutable properties, as this answer suggests. But I checked out google's persistent example with kotlin, Room works with…
Samuel Robert
  • 7,119
  • 7
  • 34
  • 52
14
votes
4 answers

Configuration 'compile' in is deprecated, but all configurations are 'implementation'

I receive following warning while executing gradle in android: Configuration 'compile' in project ':app' is deprecated. But all my dependencies included via implementation configuration. (And modules too) Are there any "invisible" dependencies in…
Valentin Baryshev
  • 2,155
  • 3
  • 12
  • 23
14
votes
3 answers

Unit testing Rxjava observables that have a delay

I want to be able to unit test an Observable that has a delayed emission, but without actually waiting for the delay time. Is there a way to do this? I'm currently using a CountDownHatch to delay the assert, and that works fine, but increases the…
triad
  • 16,961
  • 10
  • 41
  • 47
14
votes
2 answers

How to use generics in companion object

I'd like to use generics in companion object in this way: class Foo { /* ... */ companion object { fun foo(args: List) { /* ... */ } } } Unfortunately the code above raise Unresolved reference: T error.
Mpac
  • 625
  • 1
  • 6
  • 23
14
votes
1 answer

@StringRes, @DrawableRes, @LayoutRes and so on android annotations lint check with kotlin parameters

assuming you have such data class with default parameters data class Info( @DrawableRes val iconRes: Int = 0, @StringRes val stringRes: Int = 0, @LayoutRes val layoutRes: Int = 0) so you can create…
Beloo
  • 8,905
  • 6
  • 31
  • 66
14
votes
1 answer

Recyclerview: listen to padding click events

I have an horizontal RecyclerView with leftPadding = 48dp, topPadding = 24dp and clipToPadding = false. It starts with an empty space on the left, but when the user scrolls the list its items are drawn on that (previously empty) space. The top space…
14
votes
4 answers

Android Room error: TypeConverter not recognised for List of Enums

The Room library is not recognizing a TypeConverter I created for a List of enums. However, when I change this to an ArrayList of enums it works fine. Anyone has any idea why and what can I do to make this work with List? (Using List in Kotlin is…
Franco
  • 2,408
  • 1
  • 17
  • 27
14
votes
1 answer

java static final in kotlin: Const 'val' initializer should be a constant value

In Java, We can do this: public class TestA { public static final boolean flag = true; public static final String str = flag ? "A" : "B"; // ok } But cannot in Kotlin class TestA { companion object { const val flag = true …
user4003256
1 2 3
99
100