java - Android - Creating a PopupMenu on clicking an ActionBar button -
i trying make popupmenu appear after click button in action bar.
i have got buttons in file - action.xml in 'menu' folder.
this contains buttons actionbar.
<item android:id="@+id/overflow" android:icon="@drawable/ic_action_overflow" android:orderincategory="2" android:menucategory="container" android:title="overflow button" android:showasaction="ifroom|withtext" android:onclick="**showpopup**" /> <item android:id="@+id/add" android:icon="@drawable/add" android:title="add button" android:orderincategory="100" android:showasaction="ifroom|withtext" android:onclick="**showpopup**" /> once button clicked wish have popupmenu shown. showpopup method located in fragmentactivity class:
public void showpopup(menuitem v) { popupmenu popup = new popupmenu(this, this.getcurrentfocus()); popup.inflate(r.layout.pop); popup.show(); } the pop.xml file stored in layout folder (and contains view of pop menu)
unfortunately giving me runtimeexception java.lang.reflect.invocationtargetexception.
any ideas?
help appreciated.
first of all, quite easy.
you have add attribute actionbar item.
android:onclick="openpopup" openpopup popup method.
then, in mainactivity way supposed extend actionbaractivity
have add method called onclick.
public void openpopup(menuitem item){ point p; p.x = 0; p.y = 0; showpopup(mainactivity.this, p); toast.maketext(this, "popup should open", toast.length_long).show(); } and of course showpopup method.
private void showpopup(final activity context, point p) { // inflate popup_layout.xml linearlayout viewgroup = (linearlayout) context.findviewbyid(r.id.popup); layoutinflater layoutinflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view layout = layoutinflater.inflate(r.layout.popup_layout, viewgroup); // creating popupwindow final popupwindow popup = new popupwindow(context); popup.setcontentview(layout); popup.setwidth(viewgroup.layoutparams.wrap_content); popup.setheight(viewgroup.layoutparams.wrap_content); popup.setfocusable(true); popupwidth = popup.getwidth(); // clear default translucent background popup.setbackgrounddrawable(new bitmapdrawable(this.getresources())); // displaying popup @ specified location. popup.showatlocation(layout, gravity.no_gravity, p.x, p.y); // getting reference close button, , close popup when clicked. button close = (button) layout.findviewbyid(r.id.close); close.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { popup.dismiss(); } }); } that's it.
Comments
Post a Comment