---
title: "Add listener to dynamic view in ExpandableListView"  
description: "Add listener to dynamic view in ExpandableListView"  
author: "Jayden Bell"  
published: 2014-10-17  
updated: 2014-10-17  
canonical: https://www.mindstick.com/forum/2398/add-listener-to-dynamic-view-in-expandablelistview  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Add listener to dynamic view in ExpandableListView

I have an expandable list [view](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view). I use a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) adapter to [load](https://answers.mindstick.com/qa/33737/what-is-meant-by-average-load-time) my own .xml [layout](https://www.mindstick.com/articles/12853/android-layout-and-its-type). Each [group](https://yourviews.mindstick.com/view/81323/adani-green-energy-group-bags-world-s-biggest-solar-bid) has a [Switch](https://www.mindstick.com/blog/104495/switch-bringing-new-advancements-in-the-education-management-system). I don't know how many [groups](https://www.mindstick.com/interview/34156/what-are-groups-in-signalr-and-how-can-you-use-them-to-manage-chat-rooms) will be displayed. Now I am wondering how to dynamically add a listener to each switch, in [order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) to [react](https://www.mindstick.com/articles/327892/react-why-it-just-makes-sense-for-web-development) on changes.\
**ExpandableListView [Activity](https://www.mindstick.com/forum/12894/how-to-force-recreation-of-activity-fragment-on-restore):**\

```
    public class ExpandableListView extends Activity {    private MyCustomAdapter adapter;    private ExpandableListView listView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.detail_layout);         initViews();            }       private void initViews() {        listView = (ExpandableListView) findViewById(R.id.foo);        adapter = new MyCustomAdapter(this, data); // my custom adapter        listView.setAdapter(adapter);        //...       }    //...   }
```

MyCustomAdapter\

```
public class MyCustomAdapter extends BaseExpandableListAdapter {    @Override    public View getGroupView(int groupPosition, boolean isExpanded,            View convertView, ViewGroup parent) {        String headerTitle = (String) getGroup(groupPosition);        if (convertView == null) {            LayoutInflater infalInflater = (LayoutInflater) this.context                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertView = infalInflater.inflate(R.layout.list_group, null); // load layout with switch        }        TextView lblListHeader = (TextView) convertView                .findViewById(R.id.myTextView;        lblListHeader.setTypeface(null, Typeface.BOLD);        lblListHeader.setText(headerTitle);        return convertView;    }    // ...}
```

\
**list_group.xml****\**

```
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:padding="8dp"    android:background="#000000">      <TextView        android:id="@+id/myTextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true"        android:paddingLeft="20dp"        android:paddingRight="0dp"        android:paddingEnd="0dp"        android:paddingStart="20dp"        android:textSize="17sp" />      <Switch        android:id="@+id/mySwitch"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true"        android:focusable="false"        android:checked="true"     />  </RelativeLayout>
```

```

```

How can I add a listener to each "mySwitch"? Is this possible in ExpandableListView?**\**

## Replies

### Reply by Mark Devid

Add a listener when inflating the group layout. And you can distinguish which group's switch was clicked by setting a tag that corresponds to that group position.

## Here's an example:

\

```
@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {    if (convertView == null) {        ...         // Add a switch listener        Switch mySwitch = (Switch) convertView.findViewById(R.id.mySwitch);        mySwitch.setTag(groupPosition);        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                Log.e("TAG", "Clicked position: " + buttonView.getTag());            }        });    } else {        convertView.findViewById(R.id.mySwitch).setTag(groupPosition);    }     ...     return convertView;}
```

\

Note that you need to set the tag each time getGroupView is called.

\

As a sidenote: you should use a Holder pattern so you don't have to call findViewById (which is costly) in getGroupView.


---

Original Source: https://www.mindstick.com/forum/2398/add-listener-to-dynamic-view-in-expandablelistview

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
