ListView

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