Sunday, March 8, 2020

android kotlin - ListView OnItemClickListener example

ListView Item Click Listener
ListView allows us to display a vertically scrollable collection of views, primarily a simple view with some short text. Views are shown sequentially one after another. ListView is used to display a simple list of some text such as a color list, customer name list, user list, etc.

A ListView is an adapter view that does not know the type and content of views. Instead, ListView requests views on demand from the list adapter.

In order to display items in a list, we need to specify a list adapter. In this example, we used an array adapter to populate the list view with items.

Our main goal is to set a click listener for each of the items of the list view widget. So that, we can get the item clicked from the ListView widget and display more details about that item to our app user. In this tutorial, we used Kotlin as our programming language to write our android application script.

To achieve list view item click listener we used the AdapterView OnItemClickListener method. This method required four arguments that are parent, view, position, and id. The parent is the adapter view. The view is the item view and position is the position of the view in the adapter. The id is the row id of the item that was clicked.

By using this method we can get the list view clicked/selected item text also. Finally, we can show the list view selected/clicked item text on the app user interface.
MainActivity.kt

package com.cfsuman.kotlintutorials

import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity


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

        // Get the widgets reference from XML layout
        val listView = findViewById<ListView>(R.id.listView)
        val textView = findViewById<TextView>(R.id.textView)

        // list to populate list view
        val list = mutableListOf(
            "African violet",
            "Alloy orange",
            "Amaranth pink",
            "Apple green",
            "Beau blue",
            "Black chocolate"
        )

        // Initialize an array adapter
        val adapter = ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,list
        )

        // Attach the array adapter with list view
        listView.adapter = adapter

        // List view item click listener
        listView.onItemClickListener = AdapterView.OnItemClickListener {
                parent, view, position, id ->
            val selectedItemText = parent.getItemAtPosition(position)
            textView.text = "Selected : $selectedItemText"
        }
    }
}
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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples