I am trying to implement an action bar in which one of the buttons on click shows a popup menu. Here's the menu. XML (menu items in the action bar)
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search"
android:icon="@drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="@string/menu_search"/>
<item
android:id="@+id/refresh"
android:icon="@drawable/ic_action_refresh"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_refresh"/>
<Item
android:id="@+id/popup"
android:icon="@drawable/ic_action_search"
android:onClick="showPopup"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_search" />
I wish to show a popup menu on the click of the item having id "@+id/popup".
here is the XML for the popup menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item1"
android:icon="@drawable/ic_action_search"
android:orderInCategory="0"
android:showAsAction="always"
android:title="@string/menu_search"/>
<item
android:id="@+id/item2"
android:icon="@drawable/ic_action_search"
android:orderInCategory="1"
android:showAsAction="always"
android:title="@string/menu_search"/>
here is the onClick method for the button
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.overflow, popup.getMenu());
popup.show();
}
And the problem is that no popup shows up on click of that button. Need help folks.
As the popup menu is a MENU, you have to handle this by implementing the "onOptionsItemSelected". You'll be able to say what to do for each menu option. It will replace the "onClick" option you defined and will be called automatically.
may be you should change it to
android:onClick="showPopup"
?I found a solution to this. Instead to using the menu XML to inflate the popup menu, I made a XML layout file.
and i changed the onClick method
This solved the issue
I found this here: http://developer.android.com/guide/topics/ui/menus.html
You can put a menu within a menu to present sub-menus when the item is clicked. Then, in Java, you can use the same methods as usual.
The id of 'top' in the xml is still recognized even though it is a sub menu. This worked for me and it looks just like the popup menu.
Finally i
Try to change 'this' to getActivity().
Hope it helps..!!