android - MenuItem's onclick method is not called if menu item showAsAction="always" -
im having trouble menu items.
i have search view in menu, , set onmenuitemclicklistener
on item in onprepareoptionsmenu
.
and menu xml: showasaction attribute set "always"
however, if click search icon, nothing happen, toast did not show up.
strange thing if set showasaction="always|collapseactionview", if work, search icon gone , replaced "search" text.
it works, toast shown.
but icon gone
********************edit****************************************
you using wrong syntax menu. kindly replace menu inflater/handler code this.
@override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.youractivity_menu, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.search) { //handle onclick toast on here. //if have multiple options replace if switch() statement return true; } return super.onoptionsitemselected(item); }
as far icon concerned, have specify icon attribute in xml file adding android:icon="@drawable/you_icon_here"
edit: have @ http://developer.android.com/training/search/setup.html supposed keep android:showasaction="collapseactionview|ifroom" work. change have make add android:icon attribute xml
<item android:id="@+id/search" android:title="@string/search_title" android:icon="@drawable/ic_search" android:showasaction="collapseactionview|ifroom" android:actionviewclass="android.widget.searchview" />
or android.widget.v7 support depending on android version wish support
Comments
Post a Comment