Android10/28/2019

Android Floating Context Menu Tutorial — Long Press Menu in Android

Floating Context Menu in Android is a contextual popup menu that appears when the user performs a long press on a view.

It is commonly used for:

  • Edit options
  • Delete actions
  • Copy/Paste menus
  • Item-specific actions
  • Contextual interactions

In this tutorial, we will learn:

  • How Floating Context Menu works
  • How to register views for context menu
  • How to inflate menu XML
  • How to handle menu item clicks
  • How to add header titles
  • Modern AndroidX implementation

What Is a Floating Context Menu?

A Floating Context Menu is a popup menu displayed when a user:

  • Long presses a view
  • Long presses a list item
  • Performs contextual interaction

It provides actions related to the selected view.


Common Use Cases

  • Text editing
  • Chat message options
  • Delete/Share actions
  • File management
  • RecyclerView item actions

Important Modern Android Note

Older tutorials use:


android.support.*

which is deprecated.

Modern Android applications should use:


androidx.*

What We Will Build

In this example:

  • Long press a TextView
  • Show floating context menu
  • Handle menu selections
  • Display Toast messages

Step 1 — Create activity_main.xml

Create:


res/layout/activity_main.xml

Modern ConstraintLayout Example


<?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"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <TextView

        android:id="@+id/text_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Long Press Me"

        android:textSize="30sp"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintEnd_toEndOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Why Use ConstraintLayout?

ConstraintLayout provides:

  • Flat view hierarchy
  • Better performance
  • Responsive layouts
  • Flexible positioning

Step 2 — Create Menu XML

Create:


res/menu/example_menu.xml

example_menu.xml


<?xml version="1.0" encoding="utf-8"?>

<menu
    xmlns:android=
    "http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/option_1"

        android:title="Option 1"/>

    <item
        android:id="@+id/option_2"

        android:title="Option 2"/>

</menu>

What Is Menu XML?

Android menu XML files define:

  • Menu items
  • Icons
  • Titles
  • Actions
  • Submenus

Step 3 — Create MainActivity.java


package com.example.contextmenu;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity
        extends AppCompatActivity {

    @Override
    protected void onCreate(
            Bundle savedInstanceState
    ) {

        super.onCreate(
                savedInstanceState
        );

        setContentView(
                R.layout.activity_main
        );

        TextView textView =
                findViewById(
                        R.id.text_view
                );

        registerForContextMenu(
                textView
        );
    }

    @Override
    public void onCreateContextMenu(
            ContextMenu menu,
            View v,
            ContextMenu.ContextMenuInfo menuInfo
    ) {

        super.onCreateContextMenu(
                menu,
                v,
                menuInfo
        );

        menu.setHeaderTitle(
                "Choose your option"
        );

        getMenuInflater().inflate(
                R.menu.example_menu,
                menu
        );
    }

    @Override
    public boolean onContextItemSelected(
            @NonNull MenuItem item
    ) {

        if (
            item.getItemId()
            == R.id.option_1
        ) {

            Toast.makeText(
                    this,
                    "Option 1 Selected",
                    Toast.LENGTH_SHORT
            ).show();

            return true;

        } else if (
            item.getItemId()
            == R.id.option_2
        ) {

            Toast.makeText(
                    this,
                    "Option 2 Selected",
                    Toast.LENGTH_SHORT
            ).show();

            return true;
        }

        return super
                .onContextItemSelected(
                        item
                );
    }
}

How Floating Context Menu Works

  1. User long presses TextView
  2. Android calls onCreateContextMenu()
  3. Menu XML gets inflated
  4. Menu appears as popup
  5. User selects menu item
  6. onContextItemSelected() executes

Understanding registerForContextMenu()

This method registers a View for contextual menu interaction.

Example:


registerForContextMenu(textView);

What Is onCreateContextMenu()?

This callback creates the context menu dynamically.

Inside this method:

  • Set menu title
  • Inflate menu XML
  • Customize menu items

What Is onContextItemSelected()?

This method handles menu item clicks.

Used for:

  • Edit actions
  • Delete actions
  • Copy actions
  • Custom operations

Adding Header Title

Context menus support custom titles using:


menu.setHeaderTitle()

Example


menu.setHeaderTitle(
        "Choose your option"
);

Context Menu vs Popup Menu

Context Menu Popup Menu
Appears on long press Appears on click
Contextual actions Overflow actions
Attached to view/item Attached to anchor view

Modern Android Recommendations

Modern Android applications commonly use:

  • RecyclerView contextual menus
  • PopupMenu
  • Bottom Sheets
  • Material Design menus
  • Jetpack Compose DropdownMenu

RecyclerView Context Menu

Floating Context Menus are commonly used with:

  • RecyclerView items
  • Chat messages
  • File explorers
  • Social media posts

Jetpack Compose Alternative

Jetpack Compose now uses:


DropdownMenu

instead of XML-based context menus.


Compose Example


DropdownMenu(
    expanded = expanded,
    onDismissRequest = {}
)

Common Beginner Mistakes

1. Forgetting registerForContextMenu()

Views must be registered for context menus.


2. Using Deprecated Android Support Libraries

Always use AndroidX libraries.


3. Missing Menu XML

Context menu requires proper menu resource file.


Best Practices

  • Keep menu options minimal
  • Use meaningful contextual actions
  • Prefer Material Design menus
  • Use icons when necessary
  • Handle item clicks cleanly

FAQ

What is Floating Context Menu in Android?

A contextual popup menu shown when user long presses a view.


How do I show Context Menu?

Use:


registerForContextMenu(view)

What is the modern alternative?

PopupMenu, Bottom Sheets, and Jetpack Compose DropdownMenu are modern alternatives.


Conclusion

Floating Context Menu is a useful Android UI component for contextual interactions and quick actions.

It provides an elegant way to show item-specific actions using long press gestures.

Modern Android applications should combine AndroidX, Material Design components, RecyclerView integration, and scalable UI architecture for production-grade user experiences.


About the Author

Salil Jha is a Full Stack and Mobile Developer specializing in Android, React Native, fintech systems, scalable SaaS platforms, and developer tooling products.

CodeChain Dev — Build Modern Products. Solve Real Problems.

Deep Structural Diagnostics.

Mastering JSON is only the first step. Use our industrial-grade workbench to format, validate, and synthesize models for your production APIs.