Android Simple ListView Tutorial

In this tutorial, you will learn how to create a simple ListView in your Android application. The ListView is a view group that displays a list of vertical scrollable items. We will create a ListView that allows users to scroll the list, and on ListView item click will show results on a new activity using an intent method. So lets begin…

Create a new project in Eclipse, select FileNew > Android Application Project. Fill in the details and name your project ListViewTutorial.

Application Name : ListViewTutorial

Project Name : ListViewTutorial

Package Name : com.androidbegin.listviewtutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

package com.androidbegin.listviewtutorial;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;

public class MainActivity extends Activity {
	// Declare variables
	ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from listview_main.xml
		setContentView(R.layout.listview_main);

		// Store string resources into an Array
		String[] SamsungPhones = new String[] { "Galaxy S", "Galaxy S2",
				"Galaxy Note", "Galaxy Beam", "Galaxy Ace Plus", "Galaxy S3",
				"Galaxy S Advance", "Galaxy Wave 3", "Galaxy Wave Y",
				"Galaxy Nexus", "Galaxy W", "Galaxy Y", "Galaxy Mini",
				"Galaxy Gio", "Galaxy Wave", "Galaxy Wave 2" };

		// Locate ListView in listview_main.xml
		listView = (ListView) findViewById(R.id.listview);

		// Bind array strings into an adapter
		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, android.R.id.text1,
				SamsungPhones);
		listView.setAdapter(adapter);

		// Capture ListView item click
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {

				// Capture the click position and set it into a string
				String phones = (String) listView.getItemAtPosition(position);

				// Launch SingleItemView.java using intent
				Intent i = new Intent(MainActivity.this, SingleItemView.class);

				// Send captured string to SingleItemView.java
				i.putExtra("phones", phones);

				// Start SingleItemView.java
				startActivity(i);

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_list_view, menu);
		return true;
	}
}

Strings are stored in a string array and set into a ListView using an adapter. We used an onitemclicklistener to capture ListView item click position and pass the selected string to a new activity called SingleItemView.java.

Next, create an XML file for your MainActivity graphical layout. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file listview_main.xml and paste the following code.

listview_main.xml




    

Output:

ListView Tutorial Main XML

Next, create a new activity in a new class called SingleItemView.java. Go to File > New > Class and name it SingleItemView.java. Select your package named com.androidbegin.listviewtutorial and click Finish.

Open your SingleItemView.java and paste the following code.

SingleItemView.java

package com.androidbegin.listviewtutorial;

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

public class SingleItemView extends Activity {
	// Declare variable
	String phones;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Get the view from single_item_view.xml
		setContentView(R.layout.single_item_view);

		// Locate the TextView in single_item_view.xml
		TextView txtPhones = (TextView) findViewById(R.id.text);

		// Get string from intent passed from MainActivity.java
		Intent i = getIntent();
		phones = i.getStringExtra("phones");

		// Set the string into TextView
		txtPhones.setText(phones);

	}
}

An intent is used to retrieve a string from the previous activity and set the string into a TextView.

Next, create an XML file for your SingleItemView graphical layout. Go to res > layout > Right Click on layout > New > Android XML File

Name your new XML file singleitemview.xml and paste the following code.

singleitemview.xml



    

Next, change the application name. Open your strings.xml in your res > values folder and paste the following code.
strings.xml



    ListView Tutorial
    Hello world!
    Settings

In your AndroidManifest.xml, we need to declare an activity for SingleItemView.java. Open your AndroidManifest.xml and paste the following code.

AndroidManifest.xml



    

    
        
            
                

                
            
        
        
        
    

Output:

ListView Tutorial ScreenShot

Source Code 

[purchase_link id=”7814″ text=”Purchase to Download Source Code” style=”button” color=”green”]

Latest comments

Same Exact we need frm json or mysql DB

M Hussain Afridi

Android Simple ListView Tutorial

Great example. Thanks!

leeseawuyhs

Android Simple ListView Tutorial