Skip to main content

Set onclicklistener function on ExpandableListView android

How to get Clicked selected item from ExpandableListView in android using setOnChildClickListener.

ExpandableListView is a type of drop down list view including sub child elements included in it. On this type of list view there is setOnChildClickListener() function used at the place of onclicklistener(). So here is the complete step by step tutorial for Set onclicklistener function on ExpandableListView android .
android-project-download-code-button

How to use Set onclicklistener function on ExpandableListView android .

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.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

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");
 ParentList.add("BLOGGER");
 ParentList.add("PHOTOSHOP");
 ParentList.add("WORDPRESS");
 ParentList.add("SEO"); }
 
 // Assign children list element using string array.
 String[] AndroidName = { "ANDROID STUDIO","ANDROID EXAMPLES","ANDROID TUTORIALS" };
 String[] PhpName = { "XAMPP","PHPMYADMIN","MYSQL" };
 String[] BloggerName = { "BLOG NAME","SUB DOMAIN NAME","BLOGGER" };
 String[] PhotoshopName = { "COLOR TOOL","MOVE TOOL","CROP TOOL" };
 String[] WordPressName = { "LOGIN","ADD NEW POST","PLUGINS" };
 String[] SEOName = { "SEARCH","ENGINE","OPTIMIZATION" };
 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 if (HoldItem.equals("BLOGGER"))
 loadChild(BloggerName);
 else if (HoldItem.equals("PHOTOSHOP"))
 loadChild(PhotoshopName);
 else if (HoldItem.equals("WORDPRESS"))
 loadChild(WordPressName);
 else if (HoldItem.equals("SEO"))
 loadChild(SEOName);
 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);
 Toast.makeText(getBaseContext(), selected, Toast.LENGTH_LONG)
 .show();
 
 return true;
 }
 });
 }
 
 private void loadChild(String[] ParentElementsName) {
 ChildList = new ArrayList<String>();
 for (String model : ParentElementsName)
 ChildList.add(model);
 }

}
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 activity_main.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.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 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 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>
Screenshot:
Set onclicklistener function on ExpandableListView android

Click Here to Download Set onclicklistener function on ExpandableListView android project.

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...