Popup Menu Programatically | Android Studio | Java

How to create a Popup Menu programmatically?

The Popup Menu in Android displays the menu below the anchor text if space is available otherwise displays above the anchor text. The Popup Menu disappears if you click outside the Popup Menu. The Popup Menu displays different options/choices list so you can select any of them to perform different tasks.

>>Check For Kotlin

Video:

Step 1: Create a new project OR Open your project

Step 2: Code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <!--Selected item from popup menu will display here-->
    <TextView
        android:id="@+id/selectedTv"
        android:layout_width="wrap_content"
        android:textColor="#000"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_height="wrap_content" />

    <!--click to show popup menu-->
    <Button
        android:id="@+id/showMenuBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Widget.AppCompat.Button.Colored"
        android:text="Show Popup Menu"/>

</LinearLayout>

MainActivity.java

package com.blogspot.atifsoftwares.popupmenuprogrammatically_java;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    //UI Views
    TextView selectedTv;
    Button showMenuBtn;

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

        //init UI views
        selectedTv = findViewById(R.id.selectedTv);
        showMenuBtn = findViewById(R.id.showMenuBtn);

        //popup menu
        final PopupMenu popupMenu = new PopupMenu(this, showMenuBtn);

        //add menu items in popup menu
        popupMenu.getMenu().add(Menu.NONE, 0, 0, "Copy"); //parm 2 is menu id, param 3 is position of this menu item in menu items list, param 4 is title of the menu
        popupMenu.getMenu().add(Menu.NONE, 1, 1, "Share");
        popupMenu.getMenu().add(Menu.NONE, 2, 2, "Save");
        popupMenu.getMenu().add(Menu.NONE, 3, 3, "Delete");

        //handle menu item clicks
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                //get id of the clicked item
                int id = menuItem.getItemId();
                //handle clicks
                if (id==0){
                    //Copy clicked
                    //set text
                    selectedTv.setText("Copy clicked");
                }
                else if (id==1){
                    //Share clicked
                    //set text
                    selectedTv.setText("Share clicked");
                }
                else if (id==2){
                    //Save clicked
                    //set text
                    selectedTv.setText("Save clicked");
                }
                else if (id==3){
                    //Delete clicked
                    //set text
                    selectedTv.setText("Delete clicked");
                }
                return false;
            }
        });


        //handle button click, show popup menu
        showMenuBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupMenu.show();
            }
        });
    }
}

Step 3: Run Project

Popup Menu Programatically | Android Studio | Java


Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission | Android Studio | Kotlin

Add a Back Button to Action Bar Android Studio (Kotlin)