3

Does anyone know how I could use an ImageButton and imitate the popup of the corresponding menu? That is, to have the menu show up just below the button. Should I be using a context menu?

Basically, I'm trying to make the menu show up like below, except that I'm not using an ActionBar.

enter image description here

Kar
  • 6,063
  • 7
  • 53
  • 82

1 Answers1

8

I think you are talking about PopupMenu. If so, it can be done at the following way:

public class MyActivity extends Activity {

    private PopupMenu mPopupMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageButton imageButton = (ImageButton) findViewById(R.id.imageButton);
        mPopupMenu = new PopupMenu(this, imageButton);
        MenuInflater menuInflater = mPopupMenu.getMenuInflater();
        menuInflater.inflate(R.menu.mymenu, mPopupMenu.getMenu());
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupMenu.show();
            }
        });
    }

}

Result is the following:

the following

nikis
  • 11,166
  • 2
  • 35
  • 45