articles

Display Action Mode in Response to the User Action

Anonymous User5478 14-Sep-2015

Action mode allows the user to take actions on specific items in our user interface. Activating an ActionMode overtakes the system action bar with an overlay that includes menu options you provide and an extra option to exit the ActionMode. It also allows you to select multiple 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 and configure the above utilities.

Now create a new Android project 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 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 will appear on screen.

 Display Action Mode in Response to the User Action

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

Now let’s test the multi selection option, click on any other item, it will also get selected

 Display Action Mode in Response to the User Action

 

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

 


I am a content writter !

Leave Comment

Comments

Liked By