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

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 § ...

Audio in Android& MediaPlayer

  MainActivity.java <code> package com.example.myapplication; import androidx.appcompat.app. AppCompatActivity; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate( savedInstanceState); setContentView(R.layout. activi ty_main ); try { Uri uri = Uri. parse ( " https:// freetestdata.com/wp-content/ uploads/2021/09/Free_Test_ Data_100KB_OGG.ogg " ); MediaPlayer player = new MediaPlayer(); player.setAudioStreamType( AudioManager. STREAM_MUSIC ); player.setDataSource( this ,uri) ; player.prepare(); player.start(); } catch (Exception e) { ...

Circular Button with Icon and Text in Android

Circular Button with Icon and Text in Android Rounded android button can be used for many purposes to make awesome application. We can see many applications that have used circle button or rounded corner. In this tutorial, I am going to show how to make circular android button and to add icons/images and text in the same button. To make circular button in android app, you don’t need any java code, this can be done by using only XML. Rounded button can also be created by using java code but it is time consuming and need to have advance knowledge of java programming. Here you will learn to make circular/rounded corner button using XML only. Related: Android Button with Icon and Text Adding Badge (Item Count) to Android Button Android Switch Button Example Android Example: How to Create Circular Button with Icon and Text in Android First you have to create a new XML file in drawable folder to make rounded button. In this file I have made a rectangle and gave border radius to ma...