Add Popup menu on Button click - varshaAv/Android-Tutorials GitHub Wiki

This tutorial will help you, if you want to implement a custom action bar. with one or more floating action menu, like this...

To achieve this, first of all we will need a button on which we will perform a onclick

<Button
      android:id="@+id/menu"
      android:text="Show popup menu"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />

now create a menu file under res/menu called button_menu.xml
res/menu/button_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/i1"
    android:title="item 1"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/i2"
    android:title="item 2"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/i3"
    android:title="item 3"
    android:orderInCategory="100"
    app:showAsAction="never" />
</menu>    

now, we need to inflate this menu file on button click.so now write the following code to your desired activity.
1)in OnCreate...

Button menu=(Button)findViewById(R.id.menu);
menu.setOnClickListener(this);

2)create a showMenu()

  public void showMenu(View v)
{
    PopupMenu popup = new PopupMenu(this,v);
    popup.setOnMenuItemClickListener(this);// to implement on click event on items of menu
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.button_menu, popup.getMenu());
    popup.show();
}

3)Now call showMenu() on button click

@Override
public void onClick(View view) {
    if(view==menu)
    {
    showMenu(view);
    }

for more information about menus in android..Go to,
https://developer.android.com/guide/topics/ui/menus.html#context-menu

⚠️ **GitHub.com Fallback** ⚠️