How to create a simple ListView in Android using Kotlin. | Kotlin Tutorials



In my earlier tutorials we have seen Setting up Kotlin and a simple Programme for Toast message on Button Click.
Today we are going to look at creating a simple ListView. But before proceeding further let us clear out some of the thing about Kotlin.

In Kotlin var and val are two different keywords you can use to declare variables and properties. The difference is that using val gives you a read-only variable, which is the same as final in java. And you can declare var if you know hat you want change its value in future. So, that knowledge is enough for creating a simple ListView. Now lets head over to the code section :

But before coding the MainActivity.kt we have to make some changes in our build.gradle file i.e. as below
As you can see above you have to apply a plugin that manner to move further, after applying that plunging simply build the gradle again and then we can move further.

Now, let's make some changes in our MainActivity.kt file i.e. imports as below:

After adding the imports let's create a simple ListView in our xml file. Just copy and paste the above code to your xml file and now, let's head over to .kt file. As i have already explained about var and val above. you can change whatever you want to make changes in.
Feel free to do that all. Hope you have enjoyed the tutorial. Here is the full code for the List

activity_main.xml
MainActivity.kt
build.gradle

ScreenShot
Congratulation you are now done with creating a Simple ListView using Kotlin. If you have any questions regarding this tutorial and else, let me know in the comment box below.

Thanks...

Follow me on:

Comments

Popular Posts