Android: How to add a context menu to the item of a gridview? -
i have grid view , it's item formed icon , tow textview. code of item given below. add context menu each item, used in google playstore mentioned image link image
<relativelayout android:id="@+id/relativelayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/round_boutton" android:orientation="vertical" android:padding="5dp"> <imageview android:layout_height="96dp" android:id="@+id/imageview1" android:layout_width="96dp" android:src="@drawable/icon" android:layout_margintop="15dp" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:layout_alignparenttop="true" android:layout_centerhorizontal="true"> </imageview> <linearlayout android:id="@+id/l1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/imageview1" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:layout_margintop="15dp" android:orientation="vertical" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="4dp" android:paddingtop="4dp" android:singleline="true" android:text="textview1" android:textcolor="@color/orangemill" android:layout_weight="1" android:textsize="14dip" > </textview> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="10dp" android:textcolor="@color/white" android:maxlines="2" android:layout_weight="1" android:text="textview2" > </textview> </linearlayout> <view android:layout_width="fill_parent" android:layout_height="1dp" android:layout_below="@+id/l1" android:background="@android:color/transparent" /> </relativelayout>
create click listener menu button/image on grid item adapter class , add code create,show popup menu. implement menuitem click listener activity.
holder.menu_image.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { popupmenu popupmenu = new popupmenu(activity.this, view); popupmenu.setonmenuitemclicklistener(activity.this); popupmenu.setgravity(gravity.end); popupmenu.inflate(r.menu.menu_item); popupmenu.show(); } });
then implement menuitem selection
public boolean onmenuitemclick(menuitem item) { switch (item.getitemid()) { case r.id.action_edit: //your code break; case r.id.action_delete: //your code return true; } return true; }
add implements popupmenu.onmenuitemclicklistener on activity.
try this...
Comments
Post a Comment