Solution 1 :
You don’t need to write onCreateOptionsMenu or onOptionsItemSelected method.
materialSearchBar.getMenu().setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.action_anleitung:
//Your Action
break;
case R.id.action_fb:
//Your Action
break;
default:
return true;
}
}
});
Problem :
I hope someone can help me to solve this problem. I have an Activity with MaterialSearchBar and NoActionBar and I want to implement an options menu with items in it. The items should have their icons. So far I wrote the menu_main.xml
:
<?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">
<item
android_id="@+id/action_anleitung"
android_title="Anleitung"
android_icon="@drawable/ic_manual"
android_orderInCategory="100"
app_showAsAction="never"/>
<item
android_id="@+id/action_fb"
android_title="Feedback"
android_icon="@drawable/ic_email"
android_orderInCategory="100"
app_showAsAction="never" />
</menu>
And added it to the Main.Activity:
-
After my OnCreate-Method I added the following code:
materialSearchBar.inflateMenu(R.menu.menu_main);
and 2. added the following two methods:@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_anleitung: Toast.makeText(this, "Anleitung selected", Toast.LENGTH_SHORT).show(); return true; case R.id.action_fb: Toast.makeText(this, "Feedback selected", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } }
My problem is now the options menu is get showed and by clicking the overflow items get showed but:
-
There are no icons near the item text “Anleitung” and “Feedback”. I added already icons-drawables so it’s not a problem.
-
By clicking on the items there is no Toast with the text “Anleitung selected” or “Feedback selected”.
Did I miss something in the methods or codes? Or is it an issue of MaterialSearchBar which has another methods for options menu. I am trying to solve this problem since days and don’t find any solution
Thank you for helping out!