Android ListView in Java with Example
A ListView in Android is a type of AdapterView that displays a vertically scrollable list of items, with each item positioned one below the other. Using an adapter, items are inserted into the list from an array or database efficiently. For displaying the items in the list method setAdaptor()
is used. The setAdaptor()
method binds the adapter with the ListView.
ListView in Android is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list dynamically. The main purpose of the adapter is to retrieve data from an array or database and dynamically insert each item into the list for the desired result. Adapters can fetch data from various sources including resources like the strings.xml file.
Some Important XML Attributes of ListView
Attribute | Description |
---|---|
android:divider | A color or drawable to separate list items. |
android:dividerHeight | Divider’s height. |
android:entries | Reference to an array resource that will populate the ListView. |
android:footerDividersEnabled | When set to false, the ListView will not draw the divider before each footer view. |
android:headerDividersEnabled | When set to false, the ListView will not draw the divider before each header view. |
How to add a ListView in an Android App
Now let’s understand how to use a ListView in an Android application with an example. In the example, let’s create an Android application that will display a list of tutorials available in the GeeksforGeeks portal.
Step 1: Create a new project
- Click on File, then New => New Project.
- Choose “Empty Activity” for the project template.
- Select language as Java.
- Select the minimum SDK (According to the need).
Step 2: Modify activity_main.xml file
Add a ListView in the activity_main.xml
file and a file support_simple_spinner_dropdown_item.xml(New resource file created) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:gravity="center_vertical" />
Step 3: Modify MainActivity.java file
In this section, let’s design the backend of the application. Go to MainActivity.java
. Now in the java file create a string array and store the values you want to display in the list. Also, create an object of ListView class. In onCreate()
method find ListView by id using findViewById()
method. Create an object of ArrayAdapter using a new keyword followed by a constructor call.
The ArrayAdaptor public constructor description is below:
public ArrayAdapter (Context context, int Resource, T[ ] objects)
Parameters:
- context : current context
- Resource: resource ID for a layout file
- objects: objects to display in the ListView
According to this pass the argument in ArrayAdapter Constructor and create an object. At last, conjoin the adapter with the list using setAdapter()
method.
MainActivity file:
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ListView l;
String tutorials[] = { "Algorithms",
"Data Structures",
"Languages",
"Interview Corner",
"GATE",
"ISRO CS",
"UGC NET CS",
"CS Subjects",
"Web Technologies" };
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = findViewById(R.id.list);
ArrayAdapter<String> arr;
arr = new ArrayAdapter<String>(this,
R.layout.support_simple_spinner_dropdown_item,tutorials);
l.setAdapter(arr);
}
}
package com.gfg.listview_android
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var l: ListView
var tutorials: Array<String> = arrayOf(
"Algorithms",
"Data Structures",
"Languages",
"Interview Corner",
"GATE",
"ISRO CS",
"UGC NET CS",
"CS Subjects",
"Web Technologies"
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
l = findViewById(R.id.list)
var arr: ArrayAdapter<String> = ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,tutorials)
l.setAdapter(arr)
}
}
Output:
