---
title: "Display Action Mode in Response to the User Action"  
description: "Action mode allows the user to take actions on specific items in our user interface."  
author: "Anonymous User"  
published: 2015-09-14  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1866/display-action-mode-in-response-to-the-user-action  
category: "android"  
tags: ["android", "android activity", "android controls"]  
reading_time: 9 minutes  

---

# Display Action Mode in Response to the User Action

Action mode allows the user to take actions on specific items in our [user interface](https://www.mindstick.com/blog/302016/what-is-user-interface-and-what-are-its-benefits). Activating an ActionMode overtakes the system action bar with an overlay that includes menu [options](https://www.mindstick.com/articles/12789/best-options-for-a-home-based-business) you provide and an extra option to exit the ActionMode. It also allows you to select [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) items at once on which to apply a single action.

##### Pre-Requisites:

##### \
1. Ellipse SDK

##### \
2. Android SDK

##### \
3. ADT plugin

##### \
Or Android Studio and a compatible version of JAVA SDK

[Install](https://www.mindstick.com/articles/12876/the-top-5-reasons-to-buy-and-install-a-tonneau-cover) and configure the above utilities.

Now create a new [Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful) namely “ActionModeSample”.

##### Implementation objective:

#####

To apply action mode on List view and implement multiple item sections and operations.

##### Code Implementation:

For this, navigate to res/menu/ from package [explorer](https://yourviews.mindstick.com/view/81617/now-it-is-time-to-say-goodbye-to-internet-explorer) in Eclipse and create an xml to define the menu itself. We’ll call this one contextmenu.xml Open contextmenu.xml

```
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" >    <item        android:id="@+id/menu_delete"        android:icon="@android:drawable/ic_menu_delete"        android:title="Delete Item"/>    <item        android:id="@+id/menu_edit"        android:icon="@android:drawable/ic_menu_edit"        android:title="Edit Item"/></menu> 
```

Now navigate toMainActivity.java and add this code:

```
package com.example.contextmenusample;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.util.SparseArray;import android.util.SparseBooleanArray;import android.view.ActionMode;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.widget.AbsListView.MultiChoiceModeListener;import android.widget.AdapterView.AdapterContextMenuInfo;import android.widget.ArrayAdapter;import android.widget.ListView;
@SuppressLint("NewApi")public class MainActivity extends Activity implements MultiChoiceModeListener {                private static String[] myList = { "MindStick", "Apple", "Microsoft",                                                "Oracle", "Amazon", "Facebook", "WhatsApp" };                private ListView list;                @SuppressLint("NewApi")                @Override                protected void onCreate(Bundle savedInstanceState) {                                super.onCreate(savedInstanceState);                                setContentView(R.layout.activity_main);                                // Register a button for context events                                // Creating a list view                                list = new ListView(this);                                // Creating an array adapter                                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,                                                                android.R.layout.simple_list_item_activated_1, myList);                                // setting adapter to list view                                list.setAdapter(adapter);                                // Set up this list with a contextual ActionMode                                list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);                                list.setMultiChoiceModeListener(this);                                setContentView(list);                }
                @Override                public boolean onCreateOptionsMenu(Menu menu) {                                // Inflate the menu; this adds items to the action bar if it is present.                                getMenuInflater().inflate(R.menu.main, menu);                                return true;                }
                @Override                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {                                // List of checked item locations to do the operation                                SparseBooleanArray items = list.getCheckedItemPositions();                                // Switch on the item's ID to find the selected action                                switch (item.getItemId()) {                                case R.id.menu_delete:
                                                // Perform delete actions                                                break;                                case R.id.menu_edit:                                                // Perform edit actions                                                break;                                default:                                                return false;                                }                                return true;
                }                @SuppressLint("NewApi")                @Override                public boolean onCreateActionMode(ActionMode mode, Menu menu) {                                MenuInflater inflater = mode.getMenuInflater();                                inflater.inflate(R.menu.contextmenu, menu);                                return true;
                }                @Override                public void onDestroyActionMode(ActionMode mode) {                                // This is called when the ActionMode has been exited
                }
                @Override                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {                                // You can do extra work here to update the menu if the                                // ActionMode is ever invalidated                                return true;
                }                @Override                public void onItemCheckedStateChanged(ActionMode mode, int position,                                                long id, boolean checked) {                                int count = list.getCheckedItemCount();                                mode.setTitle(String.format("%d Selected", count));                }
}
```

##### Code Explanation:

##### · To use our ListView to activate a multiple selection ActionMode, we set its

##### choiceMode attribute to CHOICE_MODE_MULTIPLE_MODAL. This is different from

##### the traditional CHOICE_MODE_MULTIPLE, which will provide selection widgets on

##### each list item to make the selection. The modal flag applies this selection mode

##### only while an ActionMode is active.

##### \
· There are a series of callbacks required to implement an ActionMode that are

##### not built directly into an activity like the ContextMenu. We need to implement the

##### ActionMode.Callback interface to respond to the events of creating the menu and

##### selecting options. ListView has a special interface called MultiChoiceModeListener

##### which is a sub interface of ActionMode.Callback

##### \
· In onCreateActionMode(), we respond similarly to onCreateContextMenu(), just

##### inflating our menu options for the overlay to display. Your menu does not need to

##### contain icons; ActionMode can display the item names instead. The

##### onItemCheckedStateChanged() method is where we will get feedback for each

##### item selection. Here, we use that change to update the title of the ActionMode to

##### display how many items are currently selected.

##### \
· The onActionItemClicked() method will be called when the user has finished

##### making selections and taps an option item. Because there are multiple items to

##### work on, we go back to the list to get all the items selected with

##### getCheckedItemPositions() so we can apply the selected operation.

##### \

##### Running the application:

Hit on run button,\

A list of [companies](https://yourviews.mindstick.com/view/86898/tech-layoffs-us-companies-with-job-cuts) will appear on screen.

![Display Action Mode in Response to the User Action](https://www.mindstick.com/mindstickarticle/889b41fa-aa4b-45c7-b794-9c07a4a329d0/images/bc7cb24f-3c16-493b-b0af-41f13b5bf4d1.png)

Now long press on any list item and the action mode will be activated, the action bar will be overlay the action option menu for the selected item

![Display Action Mode in Response to the User Action](https://www.mindstick.com/mindstickarticle/889b41fa-aa4b-45c7-b794-9c07a4a329d0/images/458d4edc-ef29-4ef5-9f0c-daac77751df1.png)

Now let’s test the multi [selection](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) option, click on any other item, it will also get selected

![Display Action Mode in Response to the User Action](https://www.mindstick.com/mindstickarticle/889b41fa-aa4b-45c7-b794-9c07a4a329d0/images/72d0192c-525c-4c27-92c6-2c9a5eb3a07f.png)

Finally click on the tick button on the action bar and all the items will be unselected.

Thanks for reading this post.

Happy Coding!! J

---

Original Source: https://www.mindstick.com/articles/1866/display-action-mode-in-response-to-the-user-action

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
