How to Create a Contextual Action Mode Menu in Android
Contextual Action Mode in Android provides a temporary action bar that appears when users perform specific actions such as long-pressing an item.
It is commonly used in:
- File managers
- Gallery applications
- Email apps
- Chat applications
- RecyclerView item selection
Unlike a floating context menu, Contextual Action Mode displays actions directly inside the top app bar for a cleaner and more modern user experience.
In this tutorial, we will learn:
- What ActionMode is
- How to activate contextual action mode
- How to handle menu clicks
- How to customize ActionMode appearance
- Modern Android best practices
What Is Contextual Action Mode?
Contextual Action Mode is a temporary toolbar overlay that appears when the user selects or long-presses content.
It allows users to perform contextual operations such as:
- Delete
- Share
- Edit
- Copy
- Select multiple items
What We Will Build
In this Android example:
- User long-presses a TextView
- Contextual Action Bar appears
- Custom menu options are shown
- Click events are handled
- Toolbar overlay customization is applied
Step 1 — Create activity_main.xml
Create:
res/layout/activity_main.xml
<?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>
Step 2 — Create Menu Resource
Create:
res/menu/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:icon="@drawable/ic_delete"
android:title="Delete"
android:showAsAction="ifRoom"/>
<item
android:id="@+id/option_2"
android:icon="@drawable/ic_share"
android:title="Share"
android:showAsAction="ifRoom"/>
</menu>
Step 3 — Implement MainActivity.java
package com.example.contextualactionmode;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ActionMode;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity
extends AppCompatActivity {
private ActionMode actionMode;
@Override
protected void onCreate(
Bundle savedInstanceState
) {
super.onCreate(savedInstanceState);
setContentView(
R.layout.activity_main
);
TextView textView =
findViewById(R.id.text_view);
textView.setOnLongClickListener(v -> {
if (actionMode != null) {
return false;
}
actionMode =
startSupportActionMode(
actionModeCallback
);
return true;
});
}
private final ActionMode.Callback
actionModeCallback =
new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(
ActionMode mode,
Menu menu
) {
mode.getMenuInflater().inflate(
R.menu.example_menu,
menu
);
mode.setTitle(
"Choose Action"
);
return true;
}
@Override
public boolean onPrepareActionMode(
ActionMode mode,
Menu menu
) {
return false;
}
@Override
public boolean onActionItemClicked(
ActionMode mode,
MenuItem item
) {
int id = item.getItemId();
if (id == R.id.option_1) {
Toast.makeText(
MainActivity.this,
"Delete clicked",
Toast.LENGTH_SHORT
).show();
mode.finish();
return true;
}
if (id == R.id.option_2) {
Toast.makeText(
MainActivity.this,
"Share clicked",
Toast.LENGTH_SHORT
).show();
mode.finish();
return true;
}
return false;
}
@Override
public void onDestroyActionMode(
ActionMode mode
) {
actionMode = null;
}
};
}
How Contextual Action Mode Works
The workflow is:
- User long-presses a View
- ActionMode starts
- Custom contextual toolbar appears
- Menu options are displayed
- User selects action
- ActionMode closes automatically
Why ActionMode Is Useful
Contextual Action Mode improves user experience because:
- Actions appear only when needed
- UI stays clean
- Selection-based actions become intuitive
- Supports multi-selection workflows
Customizing ActionMode Appearance
Inside:
res/values/themes.xml
Add:
<style
name="Theme.MyApp"
parent="Theme.Material3.DayNight.NoActionBar">
<item
name="actionModeBackground">
@color/purple_500
</item>
<item
name="windowActionBarOverlay">
true
</item>
</style>
What Does windowActionBarOverlay Do?
When enabled:
windowActionBarOverlay = true
the contextual toolbar appears on top of the existing Toolbar instead of pushing the layout downward.
Modern Android Improvements
Modern Android applications often use Contextual Action Mode with:
- RecyclerView selection
- SelectionTracker API
- Material Design 3
- Jetpack Compose
- Toolbar integration
Common Mistakes Developers Make
1. Using Deprecated Support Libraries
Always use:
androidx
instead of old:
android.support
2. Forgetting to Finish ActionMode
Always call:
mode.finish()
after action completion.
3. Not Handling Multiple Selections Properly
RecyclerView contextual actions should properly track selected items.
ActionMode vs PopupMenu
| ActionMode | PopupMenu |
|---|---|
| Toolbar overlay UI | Floating popup menu |
| Best for selections | Best for quick actions |
| Supports multi-select | Usually single-item actions |
Modern Alternative in Jetpack Compose
Jetpack Compose applications can implement contextual toolbars using:
- TopAppBar
- Selection containers
- Compose state management
FAQ
What triggers Contextual Action Mode?
Usually long-click events or item selection.
Can ActionMode work with RecyclerView?
Yes. RecyclerView multi-selection is one of the most common ActionMode use cases.
Should modern apps still use ActionMode?
Yes. Contextual Action Mode is still widely used in Android applications for selection-based actions.
Conclusion
Contextual Action Mode provides a clean and modern way to perform temporary selection-based actions in Android applications.
Using ActionMode improves:
- User experience
- Toolbar interaction
- Selection workflows
- UI organization
Modern Android applications should combine ActionMode with RecyclerView selection systems, Material Design components, and lifecycle-aware architecture for scalable implementations.
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.