menuitem - Deeply nested menu items do not fire on click - Android -
it may worth noting r.java file disappeared morning because used incorrect menu icon name in menu xml file. have since corrected name , regenerated r.java file.
what have found out debugging:
any menu item takes 4 clicks not fire onoptionsitemselected listener in android.
can make these 4-click menuitems fire onoptionsitemselected listener? thanks.
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_sticker" android:showasaction="always" android:icon="@drawable/ic_action_add" android:title="add sticker"> <menu> <item android:id="@+id/menu_male" android:showasaction="always" android:title="male"> <menu> <item android:id="@+id/menu_add_clothes" android:showasaction="always" android:title="add clothes"> <menu> <item android:id="@+id/menu_malehat" android:showasaction="always" android:title="hat"/> <item android:id="@+id/menu_maletop" android:showasaction="always" android:title="top"/> <item android:id="@+id/menu_malepants" android:showasaction="always" android:title="pants"/> </menu> </item> <item android:id="@+id/menumaleaccessories" android:showasaction="always" android:title="add accessories"> </item> <item android:id="@+id/menumalehair" android:showasaction="always" android:title="add hair"> </item> </menu> </item> <item android:id="@+id/menu_female" android:showasaction="always" android:title="female"> <menu> <item android:id="@+id/menu_add_clothes_female" android:showasaction="always" android:title="add clothes"> <menu> <item android:id="@+id/menu_femalehat" android:showasaction="always" android:title="hat"/> <item android:id="@+id/menu_femaletop" android:showasaction="always" android:title="top"/> <item android:id="@+id/menu_femalepants" android:showasaction="always" android:title="pants"/> </menu> </item> <item android:id="@+id/menufemaleaccessories" android:showasaction="always" android:title="add accessories"> </item> <item android:id="@+id/menufemalehair" android:showasaction="always" android:title="add hair"> </item> </menu> </item> </menu> </item> <item android:id="@+id/action_settings" android:showasaction="always" android:title="sticker settings" android:icon="@drawable/ic_action_gear"> <menu> <item android:id="@+id/action_save" android:showasaction="always" android:title="@string/action_save" android:icon="@drawable/ic_action_save"> </item> <item android:id="@+id/action_cancel" android:showasaction="always" android:title="cancel" android:icon="@drawable/ic_action_cancel"> </item> <item android:title="sticker settings" android:id="@+id/action_cancel1" android:icon="@drawable/ic_action_settings"> <menu> <group android:checkablebehavior="single" > <item android:id="@+id/uniformscale" android:title="change size" android:checked="true" /> <item android:id="@+id/rotation" android:title="rotate" android:checked="false" /> </group> </menu> </item> </menu> </item> </menu>
activity:
@override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.dressing_room, menu); checkcheckboxes(menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int itemid = item.getitemid(); switch (itemid) { case r.id.menu_femalehat : addclothes(r.id.menu_femalehat); log.d("adding hat", "hat clicked"); break; case r.id.menu_femalepants: addclothes(r.id.menu_femalepants); break; case r.id.menu_femaletop: addclothes(r.id.menu_femaletop); break; case r.id.menu_malehat: addclothes(r.id.menu_malehat); break; case r.id.menu_malepants: addclothes(r.id.menu_malepants); break; case r.id.menu_maletop: addclothes(r.id.menu_maletop); break; case r.id.menumalehair: addclothes(r.id.menumalehair); break; case r.id.menufemalehair: addclothes(r.id.menufemalehair); break; case r.id.menumaleaccessories: addclothes(r.id.menumaleaccessories); break; case r.id.menufemaleaccessories: addclothes(r.id.menufemaleaccessories); break; case r.id.action_save: saveclicked();// cropimage(); break; case r.id.action_cancel: gotostartscreen();// cropimage(); break; case r.id.uniformscale: changeanisotropic(item); break; case r.id.rotation: changerotate(item); break; } return super.onoptionsitemselected(item); } private void changerotate(menuitem item){ if (item.ischecked()) { item.setchecked(false); photosorter.setrotate(2); photosorter.setanisotropic(1); } else{ item.setchecked(true); photosorter.setrotate(1); photosorter.setanisotropic(2); } } private void checkcheckboxes(menu menu){ menuitem checkbox1 = (menuitem) menu.finditem(r.id.uniformscale); if (checkbox1.ischecked()) { photosorter.setrotate(2); photosorter.setanisotropic(1); } else{ photosorter.setrotate(1); photosorter.setanisotropic(2); } } private void changeanisotropic(menuitem item){ if (item.ischecked()) { item.setchecked(false); photosorter.setanisotropic(2); photosorter.setrotate(1); } else{ item.setchecked(true); photosorter.setanisotropic(1); photosorter.setrotate(2); } }
nest <items>
of second level of <menu>
<group>
, example:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu1"/> <item android:id="@+id/menu2"> <menu> <group android:id="@+id/group2"> <item android:id="@+id/submenu1" /> <item android:id="@+id/submenu2" /> </group> </menu> </item> </menu>
Comments
Post a Comment