ListView Using ListActivity in Android

Introduction

 
This article explains the ListView with ListActivity in Android. Android Studio is used to create the sample.
 
Simply put, what we do when we create a ListView using XML is first we extend the Activity class that provides setContentView() in which we the layout id like this (R.layout.layoutid). So when we run the application it displays the list view on the screen. To show a list view on the screen without using an XML file we need to extend the ListActivity class in our class. To get the list view we call getListView() provided by the ListActivity class. Now do all the things you do when you create the list view using an XML file.
 
Step 1
 
Create the project like this:
 
Clipboard01.jpg
 
Step 2
 
Create a Java file and write this:
  1. package com.listactivtyapplication;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.app.ListActivity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.ListView;  
  12. import android.widget.Toast;  
  13. public class MainActivity extends ListActivity   
  14. {  
  15.  ListView listview;  
  16.  String[] countries = new String[]   
  17.  {  
  18.   "Samsung",  
  19.   "Nokia",  
  20.   "Blackberyy",  
  21.   "LG",  
  22.   "Motorolla",  
  23.   "Apple",  
  24.   "Karbon",  
  25.   "Micromax",  
  26.   "Zen",  
  27.  };  
  28.  @Override  
  29.  public void onCreate(Bundle savedInstanceState)   
  30.  {  
  31.   super.onCreate(savedInstanceState);  
  32.   listview = getListView();  
  33.   ArrayAdapter < String > listadapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, countries);  
  34.   listview.setAdapter(listadapter);  
  35.   listview.setOnItemClickListener(new OnItemClickListener()   
  36.   {  
  37.    @Override  
  38.    public void onItemClick(AdapterView < ? > adapterView, View view, int i, long l)  
  39.    {  
  40.     Toast.makeText(getBaseContext(), "You selected : " + countries[i], Toast.LENGTH_SHORT).show();  
  41.    }  
  42.   });  
  43.  }  
Step 3
 
Android menifect.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.listactivtyapplication"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.listactivtyapplication.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.    
  27. </manifest> 
Step 4
 
Clipboard02.jpg