Get context of PopupMenu like ContextMenu

toobsco42 picture toobsco42 · May 18, 2013 · Viewed 12k times · Source

So my ExpandableListView has group rows that are defined like :

group_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/GroupName"
        style="@style/ListViewRowStyle"
        android:paddingLeft="40dp"
        android:textSize="18sp" >
    </TextView>

    <ImageView
        android:id="@+id/Menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="10dp"
        android:contentDescription="@string/default_content_description_text"
        android:src="@drawable/ic_menu_moreoverflow_normal_holo_light" >
    </ImageView>

</RelativeLayout> 

When you click on the TextView it will expand or collapse depending on whether or not the child rows are currently displayed. I have attached an OnClickListener to the ImageView in the group row. When this ImageView is clicked I launch a PopupMenu like the images below :

enter image description here

enter image description here

Once the PopupMenu is displayed and one of the actions is clicked, I would like to perform an action on all children of the group. The problem is that I cannot determine the row in which the ImageView was clicked.

The only way I have figured out how to apply an action to all children is with a ContextMenu like the image below :

enter image description here

I want to avoid using a ContextMenu because a LongClick on a group row may not be obvious for a user to figure out that it would bring up some actions to perform on the children rows. I think the more obvious design is to anchor a PopupMenu to an ImageView (in my case a menu icon) and have the action be applied to the children rows of that group. How can I get this functionality with a PopupMenu ?

Answer

toobsco42 picture toobsco42 · May 24, 2013

So I figured out that in order to have some context of which menu icon was clicked, I utilized the setTag() and getTag() methods of the View class and just applied these methods to the ImageView (menu icon).