Reputation: 11221
There is a button in my application, I want to do that if someone click on this button, the menu will show up.
It will be like this menu on the first picture. How to do this?
Upvotes: 11
Views: 26451
Reputation: 91
private void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.option_menu, popup.getMenu());
popup.show();
}
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
showPopup(v);
}
});
Upvotes: 7
Reputation: 2664
This is how I implemented showPopUp()
function in Kotlin and I followed the same documentation that @umesh mentioned: http://developer.android.com/guide/topics./ui/menus.html#PopupMenu. Then you can call the function in your onClick()
function.
private fun showPopup(v: View) {
PopupMenu(this, v).apply {
setOnMenuItemClickListener(object: PopupMenu.OnMenuItemClickListener {
override fun onMenuItemClick(item: MenuItem?): Boolean {
return when (item?.itemId) {
R.id.settings -> {
dosomething()
true
}
else -> false
}
}
})
inflate(R.menu.menu)
show()
}
}
Upvotes: 8
Reputation: 7708
Use need to call Activity.openOptionsMenu on Button click event.
in your button click write
this.openOptionsMenu();
Upvotes: 13
Reputation: 1168
Follow below url, it has example http://developer.android.com/guide/topics/ui/menus.html#PopupMenu
Upvotes: 3