Last Updated:

 ListView in Android | Example

In this tutorial, I will show you how to create a simple ListView on Android. You'll learn what an Adapter is and what it is, as well as how to handle clicks on list items.

What is a ListView on Android?

A ListView is an item that groups multiple items and displays them in a vertical list that you can scroll through.view

List items are automatically inserted into the list by using an adapter that loads content from a variety of sources. This can be a static array of elements, a database, or something else. ListView is one of the basic and most used components of the user interface in Android. It is flexible in configuration and easy to use.

How to Use Adapter in Android

The Adapter is actually a bridge between the data source and the UI components. Data from the source (array, database) is transmitted to the adapter, after which the adapter fills the received content with various elements of the user interface, for example, in , or others.ListViewGridView

Elements such as those are subclasses and they can interact with an adapter that retrieves data from an external source and creates an element. The most used Android adapters are the .ListViewGridViewAdapterViewViewArrayAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter, BaseAdapterCursorAdapter

Working with a ListView: Handling Clicks

Android provides a method for handling clicks on . As parameters, the method takes a set of properties that define the pressed element in the .onListItemClick()ListViewListView

Example of using listView in Android

First, we need to create a simple Android project. How to do this is described here. After creating the project, you need to add the following lines to the xml file called:strings.xml

Note: using tags in the xml file, an array of strings is declared. The element of this array will be what is in the .<string-array>...</string-array><item>...</item>

Each ListView will look like a simple TextView widget. How it will look in a separate layout:listview_item.xml


  1. The data from our file needs to be represented as an array of strings (strings.xmlString[])
  2. Provide the resulting array as a parameter to the adapter.
  3. After installing the adapter, we need to add a handler for clicking on the elements.ListView
  4. In the handler, create a pop-up window that will show the text in the clicked element.ListView;

import android.content.Intent;
import android.os.Bundle;
import android.view.view;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

protected void onCreate(Bundle savedInstanceState) {
//one. Saving an array of strings from xml to String[]
String[] langs = getResources().getStringArray(R.array.languages);

//2. Set the resulting array to the adapter
this.setListAdapter(new ArrayAdapter<>(this, R.layout.list_item,, langs));

// get a reference to the ListView object
ListView listView = getListView();

//3. Set listener on listView items
listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

// get the element clicked by the user
String language = ((TextView) view).getText().toString();

//four. Create a popup Toast with the text of the clicked element
Toast.makeText(MainActivity.this, language, Toast.LENGTH_LONG).show();

That's it, you can now run the application. The result is this:


Follow the update of the Section Programming for Android