The Wayback Machine - https://web.archive.org/web/20240708095203/https://www.geeksforgeeks.org/android-listview-in-java-with-example/
Open In App

Android Listview in Java with Example

Last Updated : 18 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A ListView is a type of AdapterView that displays a vertical list of scroll-able views and each view is placed one below the other. Using adapter, items are inserted into the list from an array or database. For displaying the items in the list method setAdaptor() is used. setAdaptor() method conjoins an adapter with the list.

Android ListView 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.

The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result. So, it is the main source to pull data from strings.xml file which contains all the required strings in Java or XML files.

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 GeeksforGeeks portal.

Step 1: Create a new project

  1. Click on File, then New => New Project.
  2. Choose “Empty Activity” for the project template.
  3. Select language as Java.
  4. Select the minimum SDK as per your need.

Step 2: Modify activity_main.xml file
Add a ListView in the activity_main.xml file.

activity_main.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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>


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)

Parameter

Description

context current context
Resource the 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.java




import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
  
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);
    }
}


Output
output-screen



Similar Reads

RecyclerView using ListView in Android With Example
RecyclerView is more flexible and advanced version of ListView and GridView. RecyclerView is used for providing a limited window to a large data set, which means it is used to display a large amount of data that can be scrolled very efficiently by maintaining a limited number of Views. In RecyclerView we supply data and define how each item looks,
6 min read
Pull to Refresh with ListView in Android with Example
The SwipeRefreshLayout widget is used for implementing a swipe-to-refresh user interface design pattern. It uses a vertical swipe gesture to refresh the content of the views. SwipeRefreshLayout widget detects the vertical swipe and display distinct progress bars and triggers the callback methods in the app. To use this behavior SwipeRefreshLayout w
4 min read
Horizontal ListView in Android using Jetpack Compose
Many Android Applications present data in the form of horizontally scrollable lists. This list can be scrolled horizontally. In this article, we will take a look at How to Implement Horizontal ListView in Android Applications using Jetpack Compose. Note: If you are seeking Java code for Jetpack Compose, please note that Jetpack Compose is only avai
6 min read
View Recycling in Android with ListView
Memory management is a crucial aspect of app development. Since mobile devices have very limited memory, it is necessary to use it carefully in our applications. One of the best practices involved in doing so is "view recycling". This article is about view recycling in Android and then a simple app is created which implements the practice of view r
4 min read
How to Detect Long Press on ListView Items in Android?
ListView in Android is a ViewGroup that is used to display items in rows and has an adapter that inserts the desired elements into the list. Once the items are inserted into the ListView, they can be clicked and the desired action can be performed. As multiple operations can be performed on a ListView, in this article, we will show you how you coul
3 min read
Custom ArrayAdapter with ListView in Android
In the previous article ArrayAdapter in Android with Example, it's been discussed how the ArrayAdapter works and what are the data sources which can be attached to the ArrayAdapter with ListView. In this article, it's been discussed how to implement custom ArrayAdapter with the ListView. Have a look at the following image in which a single view in
5 min read
SearchView in Android with ListView
SearchView is a widget provided by the Android framework that allows users to search for specific data within an application. It is commonly used in apps that have large amounts of data or content that can be searched, such as contacts, music, or emails. The SearchView widget consists of an input field and a search button. Users can enter a search
2 min read
How to Search an Item in ListView using EditText and TextWatcher in Android?
Some applications provide a search bar for looking up any particular item from a list of items. Technically, a search bar is an EditText and an item list can be a ListView, RecyclerView, or a GridView that contains some items. Now, when a user types something in the EditText, the list must update in accordance with the text that the user types. Str
3 min read
How to Detect Scroll Up and Down Gestures in a ListView in Android?
A ListView in Android is a scrollable list used to display items. Data, which is a list or array of items fetched from a source or hardcoded in the application can be displayed row-wise using a ListView. If the items are less, then the list may not even reach the bottom of the screen. However, if the items are large in number, then they may go beyo
4 min read
How to Change the ListView Text Color in Android?
In Android, a ListView is a UI element used to display the list of items. This list is vertically scrollable and each item in the ListView is operable. A ListView adapter is used to supply items from the main code to the ListView in real-time. By default, the TextView font size is 14 sp and color is "@android:color/tab_indicator_text". In this arti
3 min read