java - How to insert icon for the pop up menu items -
i using popup menu inflater view menu items, not getting icons in menu list..here code please me..
<item android:id="@+id/menu_messages" android:title="messages" android:icon="@drawable/mail" /> <item android:id="@+id/menu_settings" android:title="settings" android:icon="@drawable/setting" /> <item android:id="@+id/menu_logout" android:title="logout" android:icon="@drawable/lock1" />
final imageview iv = (imageview) findviewbyid(r.id.imageview2); /** * step 1: create new instance of popup menu */ final popupmenu popupmenu = new popupmenu(this, iv);
/** * step 2: inflate menu resource. here menu resource * defined in res/menu project folder */ // popupmenu.inflate(r.layout.listview_menu); popupmenu.inflate(r.menu.example); /** * step 3: call show() method on popup menu display * menu when button clicked. */ iv.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { popupmenu.show(); } }); /** * handle menu item clicks */ popupmenu.setonmenuitemclicklistener( new popupmenu.onmenuitemclicklistener() { @override public boolean onmenuitemclick(menuitem item) { switch (item.getitemid()) { case r.id.menu_messages: startactivity(new intent(welcomescreen.this,contact.class)); break; case r.id.menu_settings: startactivity(new intent(welcomescreen.this,settings.class)); break; case r.id.menu_logout: startactivity(new intent(welcomescreen.this,login.class)); break; } return true; } });
if using popup menu copy below code , run it, icons in popupmenu popupmenu popup = new popupmenu(getapplicationcontext(), view); try { field[] fields = popup.getclass().getdeclaredfields(); (field field : fields) { if ("mpopup".equals(field.getname())) { field.setaccessible(true); object menupopuphelper = field.get(popup); class<?> classpopuphelper = class.forname(menupopuphelper .getclass().getname()); method setforceicons = classpopuphelper.getmethod( "setforceshowicon", boolean.class); setforceicons.invoke(menupopuphelper, true); break; } } } catch (exception e) { e.printstacktrace(); } popup.getmenuinflater() .inflate(r.menu.publisher, popup.getmenu()); //registering popup onmenuitemclicklistener popup.setonmenuitemclicklistener(new popupmenu.onmenuitemclicklistener() { public boolean onmenuitemclick(menuitem item) { switch (item.getitemid()) { case r.id.menu: //your function return true; default: break; } return false; } }); popup.show();
Comments
Post a Comment