Create List Using ListView – Android

Update: Instead of using ListView now you should start using RecyclerView. If you are unfamiliar with RecyclerView then you can follow this tutorial to start with RecyclerView.

Talking about the scenario where there is a need to display a list. This may be the list of options from which a user has to choose, or like a list of contact numbers. The list could be for displaying purpose or for choosing options from it.

There are two ways to create list:

  1. Using ListView
  2. Using ListActivity

In this demo application, a list would be created using ListView. In this application following points are being covered.

  • Add ListView in XML
  • Add elements in the ListView (list is ready for display)
  • Handle click event on the list items

Layout File: activity_main.xml

A ListView is added to the XML with three parameters, viz. id, layout_width, and layout_height.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_Name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Launcher Activity: MainActivity.java

In MainActivity.java, a String[] is used to store the list items. In order to populate the ListView, lvName, with list items; it needs to be referenced with the ListView of activity_main.xml. Then, an ArrayAdapter of String type is set to the lvName. The ArrayAdapter<String> requires three types of arguments, viz. Context, int, String[]. For Context type argument this@MainActivity is passed. The integer type parameter is for the layout of list items. And, the String array is for the list of items which would be populated inside the ListView lvName.

The setOnItemClickListener() adds the the listener to the list items of the ListView. And, the function of this listener is to handle the click event on the list items. The listener takes one argument of type OnItemClickListener defined inside “android.widget.AdapterView“. We must override onItemClick() of OnItemClickListener interface. And, the functionality for performing actions when an item is clicked, should be added here.

package com.example.pcsalt.listviewdemo

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


class MainActivity : AppCompatActivity() {
    private lateinit var lvName: ListView
    private var name = arrayOf(
        "Android", "iOS", "BlackBerry", "Windows Phone",
        "Symbian", "Bada"
    )

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

        lvName = findViewById(R.id.lv_Name)
        lvName.adapter = ArrayAdapter(
            this@MainActivity,
            android.R.layout.simple_list_item_1, name
        )
        lvName.onItemClickListener =
            OnItemClickListener { _, _, position, _ ->
                Toast.makeText(
                    this@MainActivity,
                    name[position],
                    Toast.LENGTH_SHORT
                ).show()
            }
    }
}

Screenshots

Download source:

GitHub