Android ListView Tutorial

Total Views : 422
Zoom In Zoom Out Read Later Print

In this tutorial i will be show you how to build simple android ListView. This article is about creating ListView and launching new activity on selecting single list item and passing data to new created activity.

Android ListView is a view which groups items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter. We can provide data to adapter from multiple source such as an array or database.  The adapter pulls the items from a data source, an array for example, and then converts each item into a view and show them into the ListView.

The onListItemClick() method is used to handle the clicks on android ListView item or row. This method receives 4 parameters when a row or item is clicked:

  • ListView : ListView which hold all of items.
  • View : selected item.
  • Position : position of item in ListView.
  • Id : id of selected item.
Let’s get start by creating a project.

1. First of all create a list of items you want to show.


<string name="app_name">ListView</string>

<string-array name="country">
<item>South Africa</item>
<item>Sri Lanka</item>
<item>West Indies</item>

2 .Creating row view for ListView.


<?xml version="1.0" encoding="utf-8"?>
android:textStyle="bold" >

3. Create a class MainActivity and write following code.

package com.prodevsblog.listexample;

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) {
void init(){
String[] country = getResources().getStringArray(;
// Binding resources to ListAdapter
this.setListAdapter(new ArrayAdapter<>(this, R.layout.list_view_row,, country));

ListView lv = getListView();

//handle list item on click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// selected item
String team = ((TextView) view).getText().toString();

// Launching new Activity and sending data to home activity
Intent i = new Intent(getApplicationContext(), HomeActivity.class);
i.putExtra("country", team);


4. Demo activity for showing result of ListView item click.

package com.prodevsblog.listexample;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
void init(){
TextView txtProduct = findViewById(;
Intent i = getIntent();
String product = i.getStringExtra("country");

If you have any problem with above code you can comment or simply download our working code for this project.

See More

Latest Photos