All CategoriesAndroid App Development

Create List View using Kotlin in Android

Let’s learn how to create a list view using Kotlin in Android. But first, we’ll begin with an introduction to Kotlin.
Normally, we use Java language for creating naïve apps for Android. When you run a Java application on a virtual machine, the application will be compiled into a set of instructions called “Bytecode”. Despite, a number of languages have been introduced to run on the Java Virtual Machine (JVM) over the past years. But, the JVM looks the same for all languages while compiling the applications. Accordingly, the main idea which JVM looks for is that language features which can help developers write simpler code and overcome the loopholes of Java language. Eventually, Kotlin is one among them!

What is Kotlin?

To start with, Kotlin is a new programming language which performs do-type checking i.e., the processing and verifying the constraints of types at compile time on the JVM. This language can also be compiled in JavaScript language. It is easy to learn and can effortlessly convert your Java code into Kotlin. The primary development of Kotlin is by JetBrain programmers from Russia.
Now, let’s dive deep into Configuring Kotlin. This section walks you through creating a list view in Android using Kotlin language.

Configuring Kotlin in Android Studio

Here is the step-by-step approach for configuring Kotlin in Android Studio:

Step 1 – Install the latest version of Android Studio

First, Download and Install New/Latest Version of Android Studio. In order to check for updates, Go to Help and select Check for updates.

Step 2 – Install the Kotlin Plugin in Android Studio.

To install Kotlin Plugin, Go to file and select settings. Just Search Plugins for Kotlin in the window of Settings as shown in the below screenshot.

Download and Install Plugin in your project/app. Restart Android Studio after completing the installation.

Step 3 – Select Kotlin under Tools, and check whether the Kotlin plugin is installed or not as shown in the below screenshot:

Step 4 – Create a new Project.

To create a new project, Go to File, select New and select the New Project as shown in the screenshot.

Now, enter all the required credentials such as Application name, Company domain, package name and project location to configure your new project.

Step 5 – Select the Empty Activity. Click on Next and Finish to end the task.

Step 6 – Next, Customize the activity by giving a unique name to it.

Now, you will get a new project in Android Studio named ‘KotlinListView’.

Step 7 – Configure project into Kotlin.
First, add Kotlin dependencies into build.gradle as shown below.
build.gradle(Project: KotlinDemo)
Next, go to dependencies and add two classpaths for Kotlin as shown in the below code snippet:

Add plugins as shown in the below code snippet:
apply plugin: ‘kotlin-android’
Add dependencies for Kotlin as shown in the below code snippet:
compile “org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version

Step 8 – Add a ListView in your xml file.


Step 9 – Next, Add Custom TextView for add items in the ListView.


Step 10 – Now, Convert Java code into Kotlin.

Now, we will convert our Java code into Kotlin, Go to Code > Convert Java File to Kotlin File as shown below (Press Alt+Ctrl+Shift+k).

Once, the conversion is done. You will get the code in Kotlin as shown in the below code snippet:

Step 11 – Take a ListView onCreate() and assign it:

val lv = findViewById( as ListView

Here we will use val keyword for assigning Android List
Take a List Adapter class, extend it into BaseAdapter and add values which you want to display into the Android ListView as below:

Here are some of the methods which we have taken:

  • getCount() for getting a size of List from array
  • getItem() for getting a position of List Item
  • getItemId() for assigning an Id on particular position
  • getView() for displaying array of items into List

Step 12 – Run your app on the emulator.

You will get an output as shown in the below image:
Output: A ListView of items will be displayed.

Source Code

Explore the Kotlin source code for this project from Github,
Hope this blog helped you in understanding creating ListView using Kotlin in Android with the code snippets.
Stay tuned! We are coming soon with more Kotlin-Android blogs. Happy Coding!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles