popup menu android

Popup Menu

Android menu is an easy and flexible menu style which is used to handle a set of actions. A popup menu displays a list of items in a vertical list that’s anchored to the view that invoked the menu.

Android popup menu appears below the anchor view if there is room, or above the view otherwise. Popup menu provides an overflow-style menu for actions that relate to specific content (such as Gmail’s email headers). PopupMenu is available from API level 11 .

1. Create a New Project :

Create a new project in Android Studio from File ⇒ New Project and select Empty Activity from templates to create a Popup Menu in android using android studio.

2. Add Button to your activity_main.xml or main layout

Add Button to your main layout. 

<Button
        android:id="@+id/popupbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Popup Menu"/>

Now open res -> layout -> activity_main.xml  file and just implement the following xml code .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/popupbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Popup Menu"/>

</RelativeLayout>

3. Define a XML menu resource file :

Create a menu resource file for the menu items

<?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/notification"
        android:title="@string/notification"/>
    <item android:id="@+id/help"
        android:title="@string/help"/>
    <item android:id="@+id/setting"
        android:title="@string/setting"/>
    <item android:id="@+id/logout"
        android:title="@string/logout"/>
</menu>

4. Implement  setOnClickListener( ) to the button

popupbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //On clicking this button pop up menu will be appear
                popupMenu(v);
            }
        });

5. Create an instance of PopupMenu

Instantiate a PopupMenu with its constructor, which takes the Context of current application and the View to which the menu should be anchored.

PopupMenu popup = new PopupMenu(this,v );

6. Inflating the menu :

Use MenuInflater to inflate the menu resource (defined in XML) into the PopupMenu object returned by PopupMenu.getMenu().

popup.getMenuInflater().inflate(R.menu.options_menu, popup.getMenu());

7. Handling menu item clicks :

Now implement the setOnMenuItemClickListener() into the PopupMenu object to handle all the menu item clicks in one place, and determine which menu item was clicked.

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.notification :
                        Toast.makeText(MainActivity.this,"Notification", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.help :
                        Toast.makeText(MainActivity.this,"Help",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.setting :
                        Toast.makeText(MainActivity.this,"Setting",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.logout :
                        Toast.makeText(MainActivity.this,"Logout",Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;

                }
                return false;
            }
        });

8. Call PopupMenu.show() :

To show the popup menu call PopupMenu.show() .

popup.show();

9. Now open your MainActivity.java and add following code :

MainActivity.java

package net.technxt.popupmenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button popupbtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        popupbtn = (Button)findViewById(R.id.popupbtn);
        popupbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //clicking this button pop up menu will be appear
                popupMenu(v);
            }
        });
    }

    private void popupMenu(View v) {
        //Creating the instance of PopupMenu
        PopupMenu popup = new PopupMenu(this,v );
        // to inflate the menu resource (defined in XML) into the PopupMenu
        popup.getMenuInflater().inflate(R.menu.options_menu, popup.getMenu());
        //popup with OnMenuItemClickListener
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.notification :
                        Toast.makeText(MainActivity.this,"Notification", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.help :
                        Toast.makeText(MainActivity.this,"Help",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.setting :
                        Toast.makeText(MainActivity.this,"Setting",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.logout :
                        Toast.makeText(MainActivity.this,"Logout",Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;

                }
                return false;
            }
        });

        popup.show();//show the popup menu

    }
}

Now run the project/app to test Popup Menu in android through android device or any Emulator .

%d bloggers like this: