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.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.app.Activity;
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;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     context=this;
     editText=(EditText)findViewById(R.id.txtedit);
     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());
          
           view.setSelected(true);
           return true;
        }
     });
   }


   @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 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 == R.id.action_settings) {
        return true;
     }
     return super.onOptionsItemSelected(item);
   }
  
   private class AcMode implements Callback{
   // Called when the user selects a contextual menu item
     @Override
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
           case R.id.itemcopy:
             copyText();
             mode.finish(); // Action picked, so close the CAB
             return true;
           case R.id.itempaste:
             pasteText();
             mode.finish(); // Action picked, so close the CAB
             return true;
           default:
             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.
     @Override
     public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
     }
   // Called when the user exits the action mode
     @Override
     public void onDestroyActionMode(ActionMode mode) {
     actionMode=null;
     }
    
   // Called when the action mode is created; startActionMode() was called
     @Override
     public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.contextmenu, menu);
        return true;
     }
   }
  
   public void copyText(){
   //save data to clipboard
   ClipData clipData = ClipData.newPlainText(editText.toString(), editText.getText().toString());
   clipboardManager.setPrimaryClip(clipData);
  
   }
  
   public void pasteText(){
   //read data from clipboard and insert it into the EditText at the cursor position
   ClipData clipData=clipboardManager.getPrimaryClip();
   try{
   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;
   editText.setText(newText);
   }catch(NullPointerException e){}
     
  
   }
}

Android-contextual-action-bar

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.