3

Right now I have a couple of buttons that do different things when clicked, but now I want one of them to display a menu when clicked, but I am not sure how to do this.

My code is something like this for the main buttons:

public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()) {
            case R.id.button1:
                //do stuff
                break;
            case R.id.button2:
                //display menu
                break;
}

If button2 is pressed, I want to display a list of options and see what menu item the user selects, but how can I do this?

Displaying icon in menu XML

   <item
        android:id="@+id/item_1"
        android:icon="@drawable/settings"
        android:showAsAction="always"
        android:title="Add item1" />
swag antiswag
  • 329
  • 3
  • 12

2 Answers2

5

You could use a PopupMenu In your onOptionsItemSelected() which will then show a different menu when one of your menu buttons is clicked. Modify this piece of code according to your needs:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.button1:

            // DO SOMETHING HERE
            break;

        case R.id.button2:

            // THE R.id.button2 has to be the same as the item that will trigger the popup menu.
            View v = findViewById(R.id.button2);
            PopupMenu pm = new PopupMenu(LoginActivity.this, v);
            pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());
            pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    Toast.makeText(getApplicationContext(), String.valueOf(item.getTitle()), Toast.LENGTH_SHORT).show();
                    switch (item.getItemId())   {

                        case R.id.menuEdit:
                            break;

                        case R.id.menuDetails:
                            break;

                        case R.id.menuDelete:
                            break;

                        default:
                            break;
                    }
                    return true;
                }
            }); pm.show();

            break;

        default:
            break;
    }

    return false;
}

You will notice that a new menu XML has been inflated at this line:

pm.getMenuInflater().inflate(R.menu.pm_accounts_item, pm.getMenu());

You will have to create a second menu XML with the list of options that you need to display when one of the buttons is clicked. This is similar to your current menu XML with the difference being, a different set of options.

IMPORTANT! Do not forget to include this View v = findViewById(R.id.button2); before the PopupMenu pm..... The PopupMenu requires a View to anchor itself to. But the onOptionsItemSelected() method does not provide this at all. Hence the extra statement.

The above example illustrates the example in an Activity. To use this in a Fragment, change the View v = findViewById(R.id.button2); to View v = getActivity().findViewById(R.id.button2);

This is the final result:

enter image description here

Siddharth Lele
  • 26,905
  • 15
  • 91
  • 144
  • Is it possible to show an icon for each item as well? I have tried doing linking each item to an icon, but only the text name of the icon shows up. I have added what I tried in my post – swag antiswag Feb 14 '16 at 20:00
  • @swagantiswag: Sorry I was away all this while. The default PopupMenu does not support icons. But, there are a few solutions here on SO that have had success doing just that. Check these: http://stackoverflow.com/q/6805756/450534, http://stackoverflow.com/q/15454995/450534. There is a github Gist that show icons here: https://gist.github.com/mediavrog/9345938 – Siddharth Lele Feb 15 '16 at 05:26
0

If you are talking about Opening option menu, Then there is a method openOptionMenu() in Activity class.

case R.id.button2:
            openOptionsMenu();
            break;

If you are talking about opening contextMenu You have to call openContextMenu() method, But don't forget to add registerForContextMenu(view) and other methods for taking action

Shree Krishna
  • 7,883
  • 6
  • 34
  • 66