All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - 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"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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/clickBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0F9D58"
        android:text="Click Me"
        android:textColor="#ffffff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

XML code 2

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/java"
        android:title="Java" />
    <item
        android:id="@+id/kotlin"
        android:title="Kotlin" />
    <item
        android:id="@+id/android"
        android:title="Android" />
    <item
        android:id="@+id/react_native"
        android:title="React Native" />
</menu>

Java code

import androidx.appcompat.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 button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.clickBtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popupMenu = new PopupMenu(MainActivity.this, button);
                popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        Toast.makeText(MainActivity.this, "You Clicked " + menuItem.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });
                popupMenu.show();
            }
        });
    }
}
  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 MainActivity.java file, We will get the reference to the Button and initialise it in the MainActivity.java 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 🡪