Pages Navigation Menu

Coding is much easier than you think

How To Create List View in an Android Application

How To Create List View in an Android Application

In this Tutorial, you will Learn How to view the activity using scroll view?

Here you go!!!!

 

 

Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

Listview: 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.

Adapter: An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

Array- Adapter: A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also take a field id. That field id should reference a TextView in the larger layout resource. However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

The tutorial is about how to display contents in activity in the list view.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to android application project, to create a new project in Eclipse refer Creation of Android Project.

Coding:

Now let’€™s go to the coding section. This project requires following files.

Source Code:

  1. Listview.java

Activity Files:

  1. activity_listview.xml-€“ listview activity (Main activity)
  2. activity_rows.xml – activity for viewing text in rows

res – Values:

  1. strings.xml -€“ strings available in list view activity

Manifest file:

  1. AndroidManifest.xml

Here is the coding for the above files.

Listview.java:

package com.simplecodestuffs.listview; // Package listview
import java.util.ArrayList; // For array list
import java.util.Arrays; // for arrays
import android.app.Activity; // Required to create an activity	
import android.os.Bundle; // A mapping from String values to various Parcelable types
import android.widget.ArrayAdapter; // expects provided resource id references a single TextView
import android.widget.ListView; // shows items in a vertically scrolling list
public class Listview extends Activity { // all classes extends activity
  private ListView mainListView ; // object for Listview class
  private ArrayAdapter<String> listAdapter ;// object for ArrayAdapter
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) { // Creates a new activity
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listview); // displays main activity (listview) when app starts
        // Find the ListView resource. 
    mainListView = (ListView) findViewById( R.id.ListView );

    // Create and populate a List of continents
    String[] continents = new String[] { "Asia", "Australia", "Europe", "North America" };  
    ArrayList<String> continentlist = new ArrayList<String>();
    continentlist.addAll( Arrays.asList(continents) );
        // Create ArrayAdapter using the continent list.
    listAdapter = new ArrayAdapter<String>(this, R.layout.activity_rows, continentlist);
        // Add more continents. If you passed a String[] instead of a List<String> 
    // into the ArrayAdapter constructor, you must not add more items. 
    // Otherwise an exception will occur.
        listAdapter.add("Antartica");
    listAdapter.add( "South America" );
    listAdapter.add( "Africa" );
    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter );      
  }
}

activity_listview.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Vertical orientation of Layout -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  
<!-- List view which displays all text in lists -->
	<ListView android:layout_width="fill_parent" 
	  android:layout_height="fill_parent" 
	  android:id="@+id/ListView"
	  android:text="@string/textlist">
	  
	</ListView>
	
</LinearLayout>

activity_rows.xml:

<!-- Text to be displayed in the lists -->

<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="10dp"
 android:textSize="16sp" >
</TextView>

Menu : activity_listview.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="100"
        android:title="@string/menu_settings"/>

</menu>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.simplecodestuffs.listview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.simplecodestuffs.listview.Listview"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Application name -->
    <string name="app_name">Listview</string>
<!-- Text view content -->
    <string name="textlist">List view....</string>
    <string name="menu_settings">Settings</string>

</resources>

Styles. xml:

<resources>
<! -- specify properties such as height, padding, font color, font size, background color -->
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

Run the android application:

Android applications can be run on the android devices. You can either connect hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to create a Android Virtual Device (AVD), refer Creating Android Virtual Device.

To run the application/ project, Build your project using Project –>  Build Project.

Build project

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run –>  Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched which displays the AVD on your screen.

You can see your app with the image set during the android project creation in AVD. Upon running the application, the emulator will be launched with the selected/ available AVD created on your screen.

Start Emulator

Emulator loading home page

To test your application, unlock the screen and double click on your app.

AVD Home screen

list view

Thus the android application project is executed successfully.

 


Click Here To Share This..!!
Share