Open new activity on clicking child element in ExpandableListView Android

Navigate to selected child item activity using child element click event  in ExpandableListView.

In my this tutorial i am designing an ExpandableListView and after that creating multiple new activities in project for each ExpandableListView child element so when application user clicks on child element of list view then it will open selected child element activity. So here is the complete step by step tutorial for Open new activity on clicking child element in ExpandableListView Android.

android-project-download-code-button

Brief review about this project:

  • Total number of listview parent items = 2 ( ANDROID, PHP ).
  • Total number of listview child elements = 6 ( ANDROID STUDIO, ANDROID EXAMPLES, ANDROID TUTORIALS, XAMPP, PHPMYADMIN, MYSQL ).
  • Total number of activities including MainActivity = 7 ( MainActivity, Android_Activity, ANDROID_EXAMPLES_Activity, ANDROID_TUTORIALS_Activity, MYSQLActivity, PHPMYADMINActivity, XAMPPActivity ).

How to Open new activity on clicking child element in ExpandableListView Android.

Note: You need to create 6 more activities into your android project same as your child elements because each newly created activity open on selected child element.
Code for MainActivity.java file.

 package com.android_examples.com.expandablelistview;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

public class MainActivity extends Activity {
 
 List<String> ChildList;
 Map<String, List<String>> ParentListItems;
 ExpandableListView expandablelistView;
 
 // Assign Parent list items here.
 List<String> ParentList = new ArrayList<String>();
 { ParentList.add("ANDROID");
 ParentList.add("PHP");
 }
 
 // Assign children list element using string array.
 String[] AndroidName = { "ANDROID STUDIO","ANDROID EXAMPLES","ANDROID TUTORIALS" };
 String[] PhpName = { "XAMPP","PHPMYADMIN","MYSQL" };
 String[] ByDefalutMessage = {"Items Loading"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 ParentListItems = new LinkedHashMap<String, List<String>>();

 for ( String HoldItem : ParentList) {
 if (HoldItem.equals("ANDROID")) {
 loadChild(AndroidName);
 } else if (HoldItem.equals("PHP"))
 loadChild(PhpName);
 
 else
 loadChild(ByDefalutMessage);

 ParentListItems.put(HoldItem, ChildList);
}
 
 expandablelistView = (ExpandableListView) findViewById(R.id.expandableListView1);
 final ExpandableListAdapter expListAdapter = new ListAdapter(
 this, ParentList, ParentListItems);
 expandablelistView.setAdapter(expListAdapter);
 
 expandablelistView.setOnChildClickListener(new OnChildClickListener() {
 
 @Override
 public boolean onChildClick(ExpandableListView parent, View v,
 int groupPosition, int childPosition, long id) {
 // TODO Auto-generated method stub
 
 final String selected = (String) expListAdapter.getChild(
 groupPosition, childPosition);
 
 // Switch case to open selected child element activity on child element selection.
 
 Intent intent; 
 switch(selected){
 case "ANDROID STUDIO":
 intent = new Intent(MainActivity.this, Android_Activity.class);
 startActivity(intent);
 break;
 
 case "ANDROID EXAMPLES":
 intent = new Intent(MainActivity.this, ANDROID_EXAMPLES_Activity.class);
 startActivity(intent);
 break;
 
 case "ANDROID TUTORIALS":
 intent = new Intent(MainActivity.this, ANDROID_TUTORIALS_Activity.class);
 startActivity(intent);
 break;
 
 case "XAMPP":
 intent = new Intent(MainActivity.this, XAMPPActivity.class);
 startActivity(intent);
 break;
 
 case "PHPMYADMIN":
 intent = new Intent(MainActivity.this, PHPMYADMINActivity.class);
 startActivity(intent);
 break;
 
 case "MYSQL":
 intent = new Intent(MainActivity.this, MYSQLActivity.class);
 startActivity(intent);
 break;
 
 } 
 
 return true;
 }
 });
 }
 
 private void loadChild(String[] ParentElementsName) {
 ChildList = new ArrayList<String>();
 for (String model : ParentElementsName)
 ChildList.add(model);
 }

}

Code for activity_main.xml layout file.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.MainActivity" >

 <ExpandableListView
 android:id="@+id/expandableListView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ExpandableListView>

</RelativeLayout>

 

Code for ListAdapter.java file.

package com.android_examples.com.expandablelistview;

import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
 
public class ListAdapter extends BaseExpandableListAdapter {
 
 private Activity context;
 private Map<String, List<String>> ParentListItems;
 private List<String> Items;
 
 public ListAdapter(Activity context, List<String> Items,
 Map<String, List<String>> ParentListItems) {
 this.context = context;
 this.ParentListItems = ParentListItems;
 this.Items = Items;
 }
 
 public Object getChild(int groupPosition, int childPosition) {
 return ParentListItems.get(Items.get(groupPosition)).get(childPosition);
 }
 
 public long getChildId(int groupPosition, int childPosition) {
 return childPosition;
 }
 
 
 public View getChildView(final int groupPosition, final int childPosition,
 boolean isLastChild, View ListView, ViewGroup parent) {
 final String CoursesName = (String) getChild(groupPosition, childPosition);
 LayoutInflater inflater = context.getLayoutInflater();
 
 if (ListView == null) {
 ListView = inflater.inflate(R.layout.child_list_item, null);
 }
 
 TextView item = (TextView) ListView.findViewById(R.id.textView1);
 
 item.setText(CoursesName);
 return ListView;
 }
 
 public int getChildrenCount(int groupPosition) {
 return ParentListItems.get(Items.get(groupPosition)).size();
 }
 
 public Object getGroup(int groupPosition) {
 return Items.get(groupPosition);
 }
 
 public int getGroupCount() {
 return Items.size();
 }
 
 public long getGroupId(int groupPosition) {
 return groupPosition;
 }
 
 public View getGroupView(int groupPosition, boolean isExpanded,
 View ListView, ViewGroup parent) {
 String CoursesFull = (String) getGroup(groupPosition);
 if (ListView == null) {
 LayoutInflater infalInflater = (LayoutInflater) context
 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 ListView = infalInflater.inflate(R.layout.parent_list_item,null);
 }
 TextView item = (TextView) ListView.findViewById(R.id.textView1);
 item.setText(CoursesFull);
 return ListView;
 }
 
 public boolean hasStableIds() {
 return true;
 }
 
 public boolean isChildSelectable(int groupPosition, int childPosition) {
 return true;
 }
}

Code for Android_Activity.java file.

package com.android_examples.com.expandablelistview;
import android.app.Activity;
import android.os.Bundle;

public class Android_Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_android_);
 }
}

 

Code for activity_android_.xml file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.Android_Activity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="ANDROID ACTIVITY "
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Code for ANDROID_EXAMPLES_Activity.java file.

package com.android_examples.com.expandablelistview;
import android.app.Activity;
import android.os.Bundle;

public class ANDROID_EXAMPLES_Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_android__examples);
 }
}

 

Code for activity_android__examples.xml file.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.ANDROID_EXAMPLES_Activity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="ANDROID EXAMPLES ACTIVITY "
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Code for ANDROID_TUTORIALS_Activity.java file.

package com.android_examples.com.expandablelistview;
import android.app.Activity;
import android.os.Bundle;

public class ANDROID_TUTORIALS_Activity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_android__tutorials);
 }
}

 

Code for activity_android__tutorials.xml layout file.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.ANDROID_TUTORIALS_Activity" >

 
 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="ANDROID TUTORIALS ACTIVITY "
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />
 

</RelativeLayout>

Code forMYSQLActivity.java file.

package com.android_examples.com.expandablelistview;
import android.app.Activity;
import android.os.Bundle;

public class MYSQLActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_mysql);
 }
}

 

Code for activity_mysql.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.MYSQLActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="MYSQL ACTIVITY "
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Code for PHPMYADMINActivity.java file.

package com.android_examples.com.expandablelistview;
import android.app.Activity;
import android.os.Bundle;

public class PHPMYADMINActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_phpmyadmin);
 }
}

Code for activity_phpmyadmin.xml layout file.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.PHPMYADMINActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="PHPMYADMIN ACTIVITY "
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Code for XAMPPActivity.java file.

package com.android_examples.com.expandablelistview;
import android.app.Activity;
import android.os.Bundle;

public class XAMPPActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_xampp);
 }

}

 

Code for activity_xampp.xml layout file.

 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.expandablelistview.XAMPPActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="XAMPP ACTIVITY "
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />
 
</RelativeLayout>

Code for child_list_item.xml layout file.

 <?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="vertical"
 android:padding="40dp" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Medium Text"
 android:textAppearance="?android:attr/textAppearanceMedium"
 />

</LinearLayout>

 

Code for parent_list_item.xml layout file.

 <?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="vertical" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Medium Text"
 android:textAppearance="?android:attr/textAppearanceMedium"
 android:padding="30dp" />

</LinearLayout>

Code for AndroidManifest.xml file.

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

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

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name=".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
 android:name=".Android_Activity"
 android:label="@string/title_activity_android_" >
 </activity>
 <activity
 android:name=".ANDROID_EXAMPLES_Activity"
 android:label="@string/title_activity_android__examples_" >
 </activity>
 <activity
 android:name=".ANDROID_TUTORIALS_Activity"
 android:label="@string/title_activity_android__tutorials_" >
 </activity>
 <activity
 android:name=".XAMPPActivity"
 android:label="@string/title_activity_xampp" >
 </activity>
 <activity
 android:name=".PHPMYADMINActivity"
 android:label="@string/title_activity_phpmyadmin" >
 </activity>
 <activity
 android:name=".MYSQLActivity"
 android:label="@string/title_activity_mysql" >
 </activity>
 </application>

</manifest>

Screenshots:

ExpandableListView

Open new activity on clicking child element in ExpandableListView Android

phpmyadmin_activity

android_examples_activity

Click Here To Download Open new activity on clicking child element in ExpandableListView Android project.

5 Comments

  1. HI,thanks for your good site
    but your code has error in this line:
    final ExpandableListAdapter expListAdapter = new ListAdapter(this, ParentList, ParentListItems);

    and the error is :
    ListAdapter is abstract; cannot be instantiated
    please fix it

  2. Dear ARYA, i have again compiled and run my whole code and it is not showing any error in Eclipse. Please download the whole project and import this project into Eclipse. If it is still showing error please let me inform by comment.

  3. I think, your codes does not work. Studio is throwing an error at this point,
    final ExpandableListAdapter expListAdapter = new ListAdapter(this, ParentList, ParentListItems)

  4. wheres the gradle in your download it cannot open in android studio

Leave a Reply

Your email address will not be published. Required fields are marked *