Wednesday, 25 September 2013

Android ListView Example

Overview: 

 In this tutorial i will show how to build simple android ListView. This tutorial is all about creating listview.


What is the ListView & Why we used it?  


ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.So to show the content in list fashion we used ListView.


Now Creating a project in eclipse for ListView.
  •  Create a new project by going to File > New > Android Application Project. Fill all the details and name of your activity,I have taken it ListViewActivity.                                                                                                
  • Copy paste all code that i have given below under your main activity called ListViewActivity.
package com.rkt.listviewexample;

import java.util.ArrayList;
import java.util.Arrays;
import com.rkt.listviewexample.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListViewActivity extends Activity {

    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.Androidlist);

        // Create and populate a List of Country names.
        String[] countries = new String[] { "India", "Iran", "Canada",
                "Turkey", "Zimbabwe", "Iceland", "Bahrain", "Australia",
                "Afghanistan" };
        ArrayList<String> al = new ArrayList<String>();
        al.addAll(Arrays.asList(countries));

        // Create ArrayAdapter using the ArrayList al.
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, al);



//here you can add items directly in listAdapter
        listAdapter.add("Malaysia");
        listAdapter.add("Mexico");
        listAdapter.add("France");
        listAdapter.add("Israel");
        listAdapter.add("Mongolia");

        // Set the ArrayAdapter as the ListView's adapter.
        mainListView.setAdapter(listAdapter);
    }
}


  • Lets start your work on UI part,you need a xml file that show our item's in list manner. So create an XML file under res folder and name it as activity_list_view.xml and paste the following code.
( Right Click on res/layout ⇒ New ⇒ Android XML File)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/Androidlist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff00" >
    </ListView>

</LinearLayout>


  •  Now you have to create one more xml file  to show the text in listview.Create an XML file under res/layout folder and name it as simplerow.xml and type the following code under this file.This file only contains the TextView to display the text.
(Again right click on res/layout ⇒ New ⇒ Android XML File

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:textColor="#191970"
    android:textColorHighlight="#ffff00"
    android:textSize="16sp"
    android:textStyle="bold" >

</TextView> 

  •  Ok,this is the time to run your project by right clicking on your project folder ⇒ Run As ⇒ Android Application.You should see the following result in your device.

Output:

 Please Share this tutorial On:

3 comments:

  1. Very simple explanation!!!!!!!! Good.

    ReplyDelete
  2. ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view that's placed into the list.Android Training in chennai | Android Training|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete