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.

enter image description here

and menu xml: showasaction attribute set "always" enter image description here

however, if click search icon, nothing happen, toast did not show up.

enter image description here

strange thing if set showasaction="always|collapseactionview", if work, search icon gone , replaced "search" text.

it works, toast shown.

enter image description here

but icon gone

enter image description here

********************edit****************************************

enter image description here

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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -