Android contextual action bar

Contextual Action Bar displays contextual actions. The Contextual Action Bar is displayed at the top of the screen when you enable the contextual action mode on a view. It allows the user to perform some actions on the selected view. It disappears when you select an action item. It is recommended to use contextual action bar to display contextual action items if you develop applications for Android 3.0 and higher, instead of the floating context menu.

To enable the contextual action mode on a view, you need to call the startActionMode() method of the current activity and pass to this method an object of a class that implements the ActionMode.CallBack interface in response to the long-click on the view. Below is the code of the MainActivity class from the previous tutorial (Android floating context menu) after it is modified to enable the contextual action mode on the EditText.

package com.example.androidexample;
import android.text.method.ScrollingMovementMethod;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
EditText editText;
ClipboardManager clipboardManager;
ActionMode actionMode;
Activity context;
   protected void onCreate(Bundle savedInstanceState) {
     editText.setText("Perform a long-click here to display the context menu.");
     // Make the EditText scrollable
     editText.setMovementMethod(new ScrollingMovementMethod());
     // Get ClipboardManager object
   clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
   // Response to long-click on the EditText
     editText.setOnLongClickListener(new View.OnLongClickListener() {
        // Called when the user long-clicks on the EditText
        public boolean onLongClick(View view) {
           if (actionMode != null) {
             return false;

           // Start the Contextual Action Bar
           actionMode =context.startActionMode(new AcMode());
           return true;

   public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
     getMenuInflater().inflate(, menu);
     return true;

   public boolean onOptionsItemSelected(MenuItem item) {
     // Handle action bar item clicks here. The action bar will
     // automatically handle clicks on the Home/Up button, so long
     // as you specify a parent activity in AndroidManifest.xml.
     int id = item.getItemId();
     if (id == {
        return true;
     return super.onOptionsItemSelected(item);
   private class AcMode implements Callback{
   // Called when the user selects a contextual menu item
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
             mode.finish(); // Action picked, so close the CAB
             return true;
             mode.finish(); // Action picked, so close the CAB
             return true;
             return false;
     // Called each time the action mode is shown. Always called after onCreateActionMode, but
     // may be called multiple times if the mode is invalidated.
     public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
   // Called when the user exits the action mode
     public void onDestroyActionMode(ActionMode mode) {
   // Called when the action mode is created; startActionMode() was called
     public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(, menu);
        return true;
   public void copyText(){
   //save data to clipboard
   ClipData clipData = ClipData.newPlainText(editText.toString(), editText.getText().toString());
   public void pasteText(){
   //read data from clipboard and insert it into the EditText at the cursor position
   ClipData clipData=clipboardManager.getPrimaryClip();
   int curPos=editText.getSelectionStart();
   String textToPaste=clipData.getItemAt(0).getText().toString();  
   String oldText=editText.getText().toString();
   String textBeforeCursor=oldText.substring(0,curPos);
   String textAfterCursor=oldText.substring(curPos);
   String newText=textBeforeCursor+textToPaste+textAfterCursor;
   }catch(NullPointerException e){}


Posted by: Dara | post date: 09-30-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.