ListView

In Android, a ListView is a type of widget used to display a vertical list of items in a scrollable format. It’s one of the most common UI components used in Android apps when we need to show a large number of similar items, such as a list of contacts, messages, or any other repetitive data. The main feature of a ListView is that it allows users to scroll through the items if they are too many to fit on the screen.

A ListView works by taking a list of data and displaying each piece of data as an individual item in the list. Each item can be customized to display different types of information, like text, images, or even complex layouts.

Step 1: Start an android project.

Step 2: Open the strings.xml file and write the following code

<resources>
    <string name="app_name">ListView Demo</string>
    <string name="txt_list">List of Cities</string>

    <string-array name="array_cities">
        <item>Bangalore</item>
        <item>Mysore</item>
        <item>Chitradurga</item>
        <item>Dharwad</item>
        <item>Belgum</item>
        <item>Mangalore</item>
        <item>Kodagu</item>
        <item>Hassan</item>
    </string-array>
</resources>

Step 3: Design the form (activity_main.xml ) as shown in the video.

activity_main.xml file

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="216dp"
        android:layout_height="57dp"
        android:layout_marginEnd="79dp"
        android:layout_marginLeft="116dp"
        android:layout_marginRight="79dp"
        android:layout_marginStart="116dp"
        android:layout_marginTop="46dp"
        android:text="@string/txt_list"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ListView
        android:id="@+id/listview_cities"
        android:layout_width="412dp"
        android:layout_height="505dp"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 4: Open MainActivity.java file and write the following code.

package com.example.part_a6_listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView_cities=(ListView)findViewById(R.id.listview_cities);

        final String[] cities=getResources().getStringArray(R.array.array_cities);
        // ArrayAdapter is a bridge between data source and UI components
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1 ,cities);

        listView_cities.setAdapter(adapter);

        listView_cities.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,"Clicked "+cities[position],Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Step 5: Run the project