Open In App

Android ListView in Kotlin

Last Updated : 29 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

ListView in Android is a ViewGroup which is used to display a scrollable list of items arranged in multiple rows. It is attached to an adapter which dynamically inserts the items into the list. The main purpose of the adapter is to retrieve data from an array or a database and efficiently push every elements into the list for a desired result.

In this article, we will learn how to use ListView of Android in Kotlin.

Android Adapter

The adapter holds the data fetched from an array and iterates through each item in the data set and generates the respective views for each item of the list. So, we can say it act as an intermediate between the data sources and adapter views such as ListView, and GridView.

Types of Adapters

Different Types of Adapter are mentioned below:

Adapter Type

Description

ArrayAdapter

It always accepts an Array or List as input. We can store the list items in the strings.xml file also.

CursorAdapter

It always accepts an instance of the cursor as an input means

SimpleAdapter

It mainly accepts static data defined in the resources like arrays or databases.

BaseAdapter

It is a generic implementation for all three adapter types and it can be used in the views according to our requirements.

Some Important XML Attributes of ListView

AttributeDescription
android:dividerA color or drawable to separate list items.
android:dividerHeightDivider’s height.
android:entriesReference to an array resource that will populate the ListView.
android:footerDividersEnabledWhen set to false, the ListView will not draw the divider before each footer view.
android:headerDividersEnabledWhen set to false, the ListView will not draw the divider before each header view.

Now, we going to create an Android application, using an array adapter. Open an activity_main.xml file from \res\layout path and write the code like as shown below.

When we have created layout, we need to load the XML layout resource from our activity onCreate() callback method and access the UI element form the XML using findViewById().

MainActivity.kt
package org.geeksforgeeks.demo

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var listView: ListView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // use ArrayAdapter and define an array
        val arrayAdapter: ArrayAdapter<*>
        val users = arrayOf(
            "Python", "Java", "C++", "PHP", "Kotlin", "GoLang", "SQL", "R Language",
            "Android", "Git", "AWS", "Docker", "Azure", "GCP", "JavaScript", "TypeScript"
        )

        // access the listView from xml file
        listView = findViewById(R.id.user_list)
        arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, users)
        listView.adapter = arrayAdapter
    }
}
activity_main.xml

Output:

This is the expected output from the above code:

Output_Application


Click Here to Learn More about Android Application Development with Kotlin



Next Article

Similar Reads

three90RightbarBannerImg