Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Popup Menu in Android With Example

A PopupMenu in Android is used to display a list of items in a vertical list anchored to a view. It's often used for providing additional actions related to a view, especially when the UI space is limited.

Here's how you can create a PopupMenu:

1. Define the Menu Items:

Create a new XML resource file (e.g., popup_menu.xml) in your res/menu directory.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_option1"
        android:title="Option 1"/>
    <item
        android:id="@+id/action_option2"
        android:title="Option 2"/>
    <item
        android:id="@+id/action_option3"
        android:title="Option 3"/>
</menu>

2. Create and Show the PopupMenu:

// Assuming this code is inside an Activity or Fragment

val anchorView: View = findViewById(R.id.anchor_button)  // the view to which the popup menu should be anchored

anchorView.setOnClickListener {
    // Create a PopupMenu
    val popupMenu = PopupMenu(this, anchorView)
    
    // Inflate the menu from the XML resource
    popupMenu.menuInflater.inflate(R.menu.popup_menu, popupMenu.menu)
    
    // Set a click listener
    popupMenu.setOnMenuItemClickListener { menuItem ->
        when (menuItem.itemId) {
            R.id.action_option1 -> {
                Toast.makeText(this, "Option 1 clicked", Toast.LENGTH_SHORT).show()
                true
            }
            R.id.action_option2 -> {
                Toast.makeText(this, "Option 2 clicked", Toast.LENGTH_SHORT).show()
                true
            }
            R.id.action_option3 -> {
                Toast.makeText(this, "Option 3 clicked", Toast.LENGTH_SHORT).show()
                true
            }
            else -> false
        }
    }
    
    // Show the menu
    popupMenu.show()
}

3. (Optional) Styling the PopupMenu:

You can style the PopupMenu by creating a custom theme in your styles.xml and applying it to the PopupMenu.

Example:

<style name="MyPopupMenu" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textColor">@color/my_text_color</item>
    <item name="android:background">@color/my_background_color</item>
    <!-- Add more styling attributes if needed -->
</style>

And apply it:

val popupMenu = PopupMenu(this, anchorView, 0, 0, 0, R.style.MyPopupMenu)

With this, you've created a simple PopupMenu in Android, added items to it, and provided actions for when each item is clicked.

  1. Creating Popup Menu in Android example code:

    // Create a button or any view
    Button popupButton = findViewById(R.id.popup_button);
    
    // Set the click listener
    popupButton.setOnClickListener(v -> {
        PopupMenu popupMenu = new PopupMenu(this, v);
        popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
    
        // Set item click listener if needed
    
        // Show the PopupMenu
        popupMenu.show();
    });
    
  2. Customizing Popup Menu in Android:

    You can customize a Popup Menu by inflating a custom layout:

    PopupMenu popupMenu = new PopupMenu(this, v);
    MenuInflater inflater = popupMenu.getMenuInflater();
    inflater.inflate(R.menu.custom_popup_menu, popupMenu.getMenu());
    
  3. PopupMenu with icons in Android example:

    Include icons in your menu XML:

    <item
        android:id="@+id/menu_item"
        android:icon="@drawable/ic_icon"
        android:title="Menu Item" />
    
  4. Android Popup Menu with RecyclerView:

    Handle item click on RecyclerView and show PopupMenu:

    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(context,
        (view, position) -> {
            PopupMenu popupMenu = new PopupMenu(context, view);
            popupMenu.inflate(R.menu.recycler_view_popup_menu);
    
            // Handle item click
    
            popupMenu.show();
        }));
    
  5. PopupMenu on long press in Android:

    Use a long press listener:

    view.setOnLongClickListener(v -> {
        PopupMenu popupMenu = new PopupMenu(context, v);
        popupMenu.inflate(R.menu.long_press_popup_menu);
    
        // Handle item click
    
        popupMenu.show();
        return true;
    });
    
  6. PopupMenu with fragments in Android example:

    You can show a PopupMenu from a Fragment in a similar way as from an Activity.

  7. PopupMenu gravity in Android example:

    Set the gravity when creating the PopupMenu:

    PopupMenu popupMenu = new PopupMenu(context, anchorView, Gravity.END);
    
  8. PopupMenu item click listener in Android:

    Set a listener for item clicks:

    popupMenu.setOnMenuItemClickListener(item -> {
        // Handle item click
        return true;
    });
    
  9. PopupMenu styling in Android:

    Customize the PopupMenu appearance using styles:

    Context wrapper = new ContextThemeWrapper(context, R.style.PopupMenuStyle);
    PopupMenu popupMenu = new PopupMenu(wrapper, anchorView);