Android Activity For Each Items In ListView

By | November 22, 2013
In the previous tutorial we have seen an example on Simple ListView and Custom ListView here in this post we will see how set an Activity for each items in listview and onclick of any items in listview must opens an activity which is associated with it .

Android Activity For Each ListItems


Lets See An Example .

Project Detail

Project Name ListitemActivity
Package com.pavan.listitemactivity
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

Activities

Create four activity these activities corresponds to each item in listview .

 ListView Items  Activity
Activity_01     Activity_01.java  
Activity_02       Activity_02.java    
Activity_03  Activity_03.java
Activity_04  Activity_04.java

File:  Activity_01.java

package com.pavan.listitemactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class Activity_01 extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText(“Activity_01”);
  tv.setTextSize(30);
  tv.setGravity(Gravity.CENTER_HORIZONTAL);
  setContentView(tv);

 }
}

File: Activity_02.java

package com.pavan.listitemactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class Activity_02 extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText(“Activity_02”);
  tv.setTextSize(30);
  tv.setGravity(Gravity.CENTER_HORIZONTAL);
  setContentView(tv);
 }
}

File: Activity_03.java

package com.pavan.listitemactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class Activity_03 extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText(“Activity_03”);
  tv.setTextSize(30);
  tv.setGravity(Gravity.CENTER_HORIZONTAL);
  setContentView(tv);
 }
}

File: Activity_04.java

package com.pavan.listitemactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

public class Activity_04 extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 
  super.onCreate(savedInstanceState);
  TextView tv = new TextView(this);
  tv.setText(“Activity_04”);
  tv.setTextSize(30);
  tv.setGravity(Gravity.CENTER_HORIZONTAL);
  setContentView(tv);
 }
}

ListView Activity

Create an xml for defining design for items in listview

File: items.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”horizontal”
    android:padding=”10dp” >

    <TextView
        android:id=”@+id/item_id”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”
        android:layout_weight=”1″
        android:textSize=”25dp” />

    <ImageView
        android:id=”@+id/fwd_img_id”
        android:layout_width=”wrap_content”
        android:layout_height=”30dp”
        android:src=”@drawable/fwd” />

</LinearLayout>

Create MainActivity  this actvity is set to ListActivity, Here in this activity we declare an array of string which conatins array of above defined activity class names and these names are set to listview items using arrayadapter

and set the onListItemClick for Listview items , on click of any items get its name and pass this fully qualified path name to Class.forName() method and use this class name inside startActivity to start corresponding activity

File: MainActivity.java

package com.pavan.listitemactivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ListActivity {

 String classes[] = { “Activity_01”, “Activity_02”, “Activity_03”,
   “Activity_04” };

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  ArrayAdapter aa = new ArrayAdapter(this,
    R.layout.items, R.id.item_id, classes);
 
  setListAdapter(aa);
 }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);
  String classname = classes[position];

  try {
   Class ourclass = Class.forName(“com.pavan.listitemactivity.”
     + classname);
   Intent i = new Intent(MainActivity.this, ourclass);
   startActivity(i);
  } catch (ClassNotFoundException e) {
  
   e.printStackTrace();
  }

 }

}

Manifest

declare the above defined activities inside AndroidManifest.xml

File : AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.pavan.listitemactivity”
    android:versionCode=”1″
    android:versionName=”1.0″ >

    <uses-sdk
        android:minSdkVersion=”8″
        android:targetSdkVersion=”17″ />

    <application
        android:allowBackup=”true”
        android:icon=”@drawable/ic_launcher”
        android:label=”@string/app_name”
        android:theme=”@style/AppTheme” >
        <activity
            android:name=”com.pavan.listitemactivity.MainActivity”
            android:label=”@string/app_name” >
          <intent-filter>
            <action android:name=”android.intent.action.MAIN” />

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

        <!– Activity_01 –>
        <activity android:name=”.Activity_01″ />
         <!– Activity_02 –>
        <activity android:name=”.Activity_02″ />
         <!– Activity_03 –>
        <activity android:name=”.Activity_03″ />
         <!– Activity_04 –>
        <activity android:name=”.Activity_04″ />
    </application>

</manifest>

  • Hi pavan simply nice tutorial,as a beginner it is more helpful for me,i really impressed at "Class ourclass = Class.forName("com.pavan.listitemactivity." + classname);" .I don't know till now,this way we can access class name and navigate to specific activity.

  • subscribe with above email subscription box with your mail to get articles directly into your inbox upon posting

  • @Azka Saman
    According to you , your passing String variable
    Actually We need to pass Class variable , not the String variable
    Because the second parameter of intent method takes Class variable

  • instead of getting the class name like this Class ourclass = Class.forName("com.pavan.listitemactivity." + classname);
    one can simply get the class name like this
    String classname = classes[position] +".class";
    and pass this value to intent..Correct me If i am wrong

    one can easily get

  • Great tutorial! I love how you used an array to instantiate the different layouts.

  • Hi Pavan
    The file was privately protected for admin , now it's safe and made public you can download and thanks for reminding

  • Is your website error prone.. actually i m unable to download code when i click on the button. my mcafee antivirus stops it .. ???