listview

Listview in Android with example | Kotlin

Android ListView is a ViewGroup that displays a list of things in multi rows and includes an adapter that inserts the items into the list automatically. The adapter’s primary function is to retrieve data from an array or database and insert each item into a list for the desired outcome.

ListView and GridView are subclasses of AdapterView, and they can be filled by binding them to an Adapter that obtains data from an external source and builds a View for each data input.

Listview in Android is commonly used to display a large amount of data, manually creating list items for the entire dataset is not practicable. Therefore, Android provides us with specific adapter classes that we can use to transfer data from the dataset to the ListView.

AttributeDescription
android: dividerIt specifies a color or a drawable to distinguish list items.
android: footerDividersEnabledThe ListView will not draw the divider before each footer view if this property is set to false.
android: headerDividersEnabledThe ListView will not draw the divider before each header view if this property is set to false.

Using Adapter in Listview


An adapter acts as a link between UI components and the data source, allowing data to be filled into the UI component. The adapter holds the data and sends it to the adapter view, which can then take the data from the adapter view and display it in various views such as a spinner, list view, grid view, and so on. The adapter retrieves items from a data source, such as an array, and converts each item into a view, which it then inserts into the ListView.

Preview: ListView in Android

<– ListView Example

How to add ListView in Android Application

  • Click on File, then New -> New Project.
  • Choose “Empty Activity” for the project.
  • Select language as Kotlin.
  • Select the minimum SDK as per your requirement.

File:activity_main.xml

–> Add the given below code to your file activity_main.xml.

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

File: string.xml

–> Add the given below code to your file string.xml

<resources>
    <string name="app_name">list_view</string>
    <string-array name="Name">

        <item>C</item>
        <item>C++</item>
        <item>Java</item>
        <item>Developers</item>
        <item>Kotlin</item>
        <item>Android</item>
        <item>Blockchain</item>
        <item>Python</item>
        <item>Java Script</item>
        <item>Php</item>
        <item>CSS</item>
        <item>DSA</item>
        <item>TypeScript</item>
        <item>Sagar Paliwal</item>
        <item>Java</item>
        <item>Dome</item>
        <item>Kotlin</item>
        <item>Android</item>
        <item>css</item>
        <item>Python</item>
        <item>Java Script</item>
        <item>Php</item>
        <item>Blockchain</item>
        <item>DSA</item>
        <item>TypeScript</item>
        <item>Sagar Paliwal</item>
        <item>Dome</item>
        <item>Kotlin</item>
        <item>Android</item>
        <item>css</item>
        <item>Python</item>
        <item>Java Script</item>
        <item>Php</item>
        <item>Blockchain</item>

    </string-array>
</resources>

File: MainActivity.kt

Let’s design the application’s backend in this section in file MainActivity.kt.

–> Add the given below code to your file MainActivity.k.

package com.example.list_view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Window
import android.view.WindowManager
import android.widget.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val language = resources.getStringArray(R.array.Name)
        val arrayAdapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, language)
        findViewById<ListView>(R.id.listView).adapter = arrayAdapter
        findViewById<ListView>(R.id.listView).onItemClickListener =
            AdapterView.OnItemClickListener { adapterView, view, position, id ->
                val selected = adapterView.getItemAtPosition(position) as String
                val itemIdPos = adapterView.getItemIdAtPosition(position)
                
                Toast.makeText(
                    applicationContext,
                    "$selected its position $itemIdPos",
                    Toast.LENGTH_LONG
                ).show()

            }
    }
}

This Post Has 3 Comments

Leave a Reply