Android Popup Menu Example

Posted on: January 13, 2018 at 2:52 pm by Sanket Mhaddalkar - No Comments

Android Popup Menu Example

 

In this article, will see how to implement Popup Menu in our Android Project. A Popup Menu displays a Menu in a modal popup window anchored to a View.

 

Popup Menu has a callback listener i.e. setOnMenuItemClickListener(), that help to notify the item clicked by user. While implementing setOnMenuItemClickListener(), we also need to implement the overriding method onMenuItemClick().

 

 

Steps and Source code for implementing Popup Menu:

 

– First Create a new Project PopupMenuExample.

 

– Create an empty activity i.e MainActivity.

 

– Now add a Button to your Activity layout file and add onClick attribute with specific name that will be added as a method in MainActivity class.

 

activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<?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="com.sanktips.popupmenuexample.MainActivity">

    <Button
        android:id="@+id/popupButton"
        android:text="Popup Menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="openPopupMenu"/>

</RelativeLayout>

 

– Create a menu folder by right clicking on res folder, New and then click on Android Resource Directory. Click on drop down of Resource Type option and select menu, click on OK button.

 

popup_menu.xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/first"
android:title="First Menu Item"/>

<item
android:id="@+id/second"
android:title="Second Menu Item"/>

<item
android:id="@+id/third"
android:title="Third Menu Item"/>

</menu>

 

– Then, In our MainActivity class we will create the method and add our Popup Menu code into it.

 

– Create instance of PopupMenu and inflate it by using xml file i.e. popup_menu.xml

 

– Then we will set the setOnMenuItemClickListener to the popupmenu into our MainActivity class to get the todo stuff done on which menu item the user have clicked.

 

MainActivity.java file:

package com.sanktips.popupmenuexample;

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


public class MainActivity extends AppCompatActivity {

    Button pBtn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pBtn = (Button) findViewById(R.id.popupButton);

    }

    //This Method invokes when user clicks the button
    public void openPopupMenu(View view){

        PopupMenu pm = new PopupMenu(MainActivity.this, pBtn);
        pm.getMenuInflater().inflate(R.menu.popup_menu, pm.getMenu());
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.first:
                        Toast.makeText(MainActivity.this, "Clicked First Menu Item", Toast.LENGTH_SHORT).show();
                        return true;

                    case R.id.second:
                        Toast.makeText(MainActivity.this, "Clicked Second Menu Item", Toast.LENGTH_SHORT).show();
                        return true;

                    case R.id.third:
                        Toast.makeText(MainActivity.this, "Clicked Third Menu Item", Toast.LENGTH_SHORT).show();
                        return true;
                }

                return true;
            }
        });
        pm.show();

    }

}

 

– So, we are done with the implementation of Popup Menu. Run the project to see the result.

 

Hope you find this article helpful. Join us on Facebook, Twitter and Google+ to get more updates on Android Development Tutorials.

 



About Author:

I am Certified Java Developer. I Develop Android Applications and Websites. I also love blogging so as to share my experiences with others.



Subscribe to our Newsletter

Join our mailing list to receive
the latest news and updates from our team.