Imitate ActionBar menu with ImageButton

Kar

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

nikis

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related