Android ListView in Kotlin
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
Attribute | Description |
---|---|
android:divider | A color or drawable to separate list items. |
android:dividerHeight | Divider’s height. |
android:entries | Reference to an array resource that will populate the ListView. |
android:footerDividersEnabled | When set to false, the ListView will not draw the divider before each footer view. |
android:headerDividersEnabled | When 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().
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
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<ListView
android:id="@+id/user_list"
android:layout_marginTop="32dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
This is the expected output from the above code:

Click Here to Learn More about Android Application Development with Kotlin