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"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraintLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.appsdeveloperblog.kotlin.examples.ListViewExample">

</android.support.constraint.ConstraintLayout>

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.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.widget.ArrayAdapter
import android.widget.ListView


class ListViewExample : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       
        setContentView(R.layout.activity_list_view_example)
        
        val constraintLayout = findViewById(R.id.constraintLayout) 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
       
       constraintLayout.addView(listView)
    }
}

 

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 *