i have menu in actionbar, checkable (in xml), when try, in java, check on press on item, item stay unchecked (but other things related action done)
my xml menu :
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> [...] <item android:id="@+id/menu_switch_full_original" android:title="@string/menu_switch_full_original" android:checkable="true"/> </menu>
and java code :
public boolean onoptionsitemselected(menuitem item) { if (mentriesids != null) { activity activity = getactivity(); switch (item.getitemid()) { [...] case r.id.menu_switch_full_original: { item.setchecked(true); [...] } activity.invalidateoptionsmenu(); } return true; }
what missing ?
try this...
private boolean ischecked = false; @override public boolean onprepareoptionsmenu(menu menu) { menuitem checkable = menu.finditem(r.id.menu_switch_full_original); checkable.setchecked(ischecked); return true; } @override public boolean oncreateoptionsmenu(menu menu) { // todo auto-generated method stub getmenuinflater().inflate(r.menu.a, menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // todo auto-generated method stub switch (item.getitemid()) { case r.id.menu_switch_full_original: ischecked = !item.ischecked(); item.setchecked(ischecked); // other functionality break; default: break; } return super.onoptionsitemselected(item); }
Comments
Post a Comment