Reputation: 1411
Im having trouble with the menu items.
I have a search view in my menu, and I set OnMenuItemClickListener
on the item in my onPrepareOptionsMenu
.
and my menu xml: the showAsAction attribute is set to "always"
however, if I click the search icon, nothing happen, the toast did not show up.
strange thing is if I set showAsAction="always|collapseActionView", if will work, but the search icon is gone and replaced by "search" text.
it works, toast is shown.
but the icon is gone
********************edit****************************************
Upvotes: 1
Views: 613
Reputation: 1587
You are using the wrong syntax for your menu. Kindly replace you menu inflater/handler code with this.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.yourActivity_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.search) {
//Handle the onClick with Toast over here.
//IF you have multiple options then replace if with a switch() statement
return true;
}
return super.onOptionsItemSelected(item);
}
As far as the icon is concerned, you have specify the icon attribute in the xml file by adding android:icon="@drawable/YOU_ICON_HERE"
EDIT: Have a look at this http://developer.android.com/training/search/setup.html You are supposed to keep android:showAsAction="collapseActionView|ifRoom" for this to work. The only change you now have to make is add an android:icon attribute to the 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 your android version you wish to support
Upvotes: 2