Create ListView in Kotlin Programmatically

With this blog post I am going to share with you how to create a ListView in Kotlin programmatically. ListView is a very commonly used UI component and knowing how to work with it enables you build even more powerful apps.

The example below will cover:

  • Create ListView in a ConstraintLayout
  • Populate ListView with data using ArrayAdapter

Layout XML file

Following is the activity_list_view_example.xml containing an empty ConstraintLayout to which we will add the ListView.

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""


Create ListView in Kotlin. Source code.

The below source code in Kotlin demonstrates how to create a new ListView, populate it with sample values and then add  ListView as a view into the ConstraintLayout.

package com.appsdeveloperblog.kotlin.examples

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView

class ListViewExample : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        val constraintLayout = findViewById( as ConstraintLayout
        val listView = ListView(this)
        val values = arrayOf(
                "Rick and Morty",
                "Gaeme of Thrones",
                "Silicon Valley",
                "IT Crowd",
                "Person of Interest")
       val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values)
       listView.adapter =adapter


You can find more similar Kotlin code examples in Kotlin category. So, if you are just starting with Kotlin and are looking for simple How to’s this category will help you find a Kotlin exampleyou are looking for.

Also, checkout the books and video courses available on Kotlin below. These resources will help you learn Kotlin and how to build mobile apps for Android with Kotlin much faster!

Happy learning Kotlin! 🙂

Kotlin. Video Courses.

The Complete Android Kotlin Developer Course

Learn how to make online games, and apps for Android O, like Pokémon , twitter,Tic Tac Toe, and notepad using Kotlin. The Complete Android Kotlin Developer Course icon

Kotlin for Java Developers

Use your Java skills to learn Kotlin fast. Enhance career prospects and master Kotlin, including Java interoperability. Kotlin for Java Developers icon

Kotlin for Beginners: Learn Programming With Kotlin

Learn Kotlin from scratch! Grasp object-orientation and idiomatic Kotlin to realize coding projects and Android apps! Kotlin for Java Developers icon

Leave a Reply

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