All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Popup Menu In Android

Last Updated : Mar 11, 2024

Popup Menu In Android

In this article we will show you the solution of popup menu in android, Menu is an important UI component in Android that is employed to provide common functionality throughout the application.

Users can enjoy smooth and predictable life experience throughout the application with the help of the menu.

To describe a set of choices and behaviour in our Android applications, humans have three different types of Menus available in Android. The following are the Android application menus:

Android Settings Menu: Alternatives for Android In an Android application, a menu is a primary selection of menu items that is helpful for behaviour that have worldwide effect on the trying to search application.

Android Context Menu: An Android Context Menu is a flying menu that appears only when the user clicks on an element for an extended period of time.

It is helpful for elements that impact the content or context frame.

Android Popup Menu: The Android Popup Menu demonstrates a list of items in a laterally list toward the perception that invoked this same menu and is useful for providing an overflow of behaviour related to particular content.

Step By Step Guide On Popup Menu In Android :-

XML code

<?xml version="1.0" encoding="utf-8"?>
        android:text="Click Me"
        app:layout_constraintTop_toTopOf="parent" />

XML code 2

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">
        android:title="Java" />
        android:title="Kotlin" />
        android:title="Android" />
        android:title="React Native" />

Java code

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 button;
    protected void onCreate(Bundle savedInstanceState) {
        button = (Button) findViewById(;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
                popupMenu.getMenuInflater().inflate(, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        Toast.makeText(MainActivity.this, "You Clicked " + menuItem.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
  1. Start a New Project.
  2. Open the activity main.xml file. In this step, we'll add a key on the keyboard to the characteristics suggest and name it clickBtn. Before we go any further, let's add some colour attributes to the app bar to make it more appealing. Add the following colour attributes to app > res > values > colors.xml. <resources> <color name="colorPrimary">#0F9D58</color><color name="colorPrimaryDark">#16E37F</color><color name="colorAccent">#03DAC5</color></resources>
  3. Make a menu directory and a menu file. First, we'll make a menu director that will house the menu file. Go to app > res > right-click > New > Android Commodity Directory and enter a name for the directory and a resource type in the menu.
  4. Used this file, We will get the reference to the Button and initialise it in the file. Add onClick behaviour towards the button and expand its popup menu.
  5. We then refer to and initialise the button, as well as assign onClick behaviour to it.
  6. The popup menu is then initialised, and the reference is set as the current context. Then, from the popup menu.xml file, inflate the popup menu.
  7. The Toast notification on menu item was then selected. The popup menu is then displayed.

Conclusion :-

Android Popup Menu: The Android Popup Menu demonstrates a list of items in a laterally list toward the perception that invoked this same menu and is useful for providing an overflow of behaviour related to particular content.

I hope this article on popup menu in android helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪