ZOFTINO.COM

Android Copy and Paste Text Example

Copy and paste text functionality in android app can be implemented using android clipboard framework. User can copy text in one app which allows selecting and copying of text and paste it in any other app which implements paste functionality using clipboard framework.

Copy and paste actions can be displayed as floating action bar (contextual action mode). When user selects text in an app which implements clipboard framework, contextual action mode can be displayed showing copy and paste actions. I’ll explain what we need to do to use contextual action mode for showing copy and past actions.

In this post, I’ll show how to implement copy and paste text with floating bar.

Copy and Paste Example Screen

To show select, copy and paste text functionality, we will use text view. This example layout has two text views, first text view is used to show select and copy text functionality and second one is used to paste the copied text from the first text view.

android clipboardmanager and copy and paste text example
 


    
    

    

    



 

Displaying Copy and Paste Actions on Selecting of Text

One of the steps in implementing copy and paste functionality is identifying text selection event, and displaying copy and paste actions. As mentioned, we are going to show copy and paste actions in contextual action mode.

For this, first thing we need to do is to make text in text view selectable. You can do so by using setTextIsSelectable method of text view.

        copytv = (TextView) findViewById(R.id.cpyTxt);
        copytv.setTextIsSelectable(true);

To display contextual action mode, you need to implement ActionMode.Callback and call activity’s startActionMode() method passing ActionMode.Callback object. To display floating contextual action mode, you need to pass, in addition to ActionMode.Callback object, action mode type ActionMode.TYPE_FLOATING to startActionMode method.

Usually, startActionMode() method is called in response to user actions to display contextual action mode. But for copy and paste text functionality, it should be displayed when user selects text. TextView widget takes care of handling text selection and showing contextual action mode with default actions if text selection is enabled.

If you want to remove default actions from context menu, add your own actions to context menu, and handle click events, you can do so by implementing ActionMode.Callback and passing ActionMode.Callback object to TextView by calling setCustomSelectionActionModeCallback method on text view.

 copytv.setCustomSelectionActionModeCallback(actionModeCallback);
 

ActionMode Callback Implementation

First you need to define menu items in menu.xml and keep it in res/menu folder.

 

    
    
    

 

Implement ActionMode.Callback’s onCreateActionMode method and inflate menu to add menu items to floating context menu. As mentioned above, you can remove menu items which are created by TextView’s default ActionMode.Callback implementation.

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {

        menu.removeItem(android.R.id.shareText);
        menu.removeItem(android.R.id.cut);

        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.copypaste, menu);
        return true;
    }

 

Implement onActionItemClicked method to handle click events of menu items in the context menu.

 @Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_copy:
            copyText();
            mode.finish();
            return true;
        case R.id.action_paste:
            pasteText();
            mode.finish();
            return true;
        default:
            return false;
    }

}
 

Copying and Adding Text to Clipboard

The main steps which need to be performed in response to copy button click event in the onActionItemClicked method of ActionMode.Callback object are capturing user selected text and then setting it to android clipboard.

To get user selected text, you can use TextView’s getSelectionStart and getSelectionEnd methods shown below. To set user selected text to clipboard, first get ClipboardManager, then create ClibData object using newPlainText method of ClibData and finally set ClibData to clipboard by calling setPrimaryClip method on ClipboardManager passing ClibData.

 private void copyText() {
    ClipboardManager clipboardManager = (ClipboardManager)
            getSystemService(Context.CLIPBOARD_SERVICE);

    CharSequence selectedTxt =  copytv.getText().subSequence(copytv.getSelectionStart(), copytv.getSelectionEnd());
    ClipData clipData = ClipData.newPlainText("zoftino text view", selectedTxt);
    clipboardManager.setPrimaryClip(clipData);
}
 

Reading Text from Clipboard and Paste

When user clicks paste action, the behavior that should be added in onActionItemClicked method of ActionMode.Callback object is to read ClibData from clibboard by calling getPrimaryClip() method on ClipboardManager and then set the data to TextView widget to show it on the screen where paste action was clicked.

 private void pasteText() {
    ClipboardManager clipboardManager = (ClipboardManager)
            getSystemService(Context.CLIPBOARD_SERVICE);

    if(clipboardManager.hasPrimaryClip()) {
        ClipData.Item item = clipboardManager.getPrimaryClip().getItemAt(0);

        CharSequence ptext = item.getText();
        pastetv.setText(ptext);
    }
}
 

Disabling Paste Action

When there is no data on the clipboard, it is a good practice to disable the paste action. To find out whether data exists on clipboard or not, call hasPrimaryClip method on clipboardManager.

 MenuItem paction = menu.findItem(R.id.action_paste);
if (!(clipboardManager.hasPrimaryClip())) {
    paction.setEnabled(false);
}else{
    paction.setEnabled(true);
}
 

ClipboardManager PrimaryClipChangedListener

If your app needs to listen to changes on the clipboard, you can do so by implementing ClipboardManager.OnPrimaryClipChangedListener and adding the listener to clipboard by calling addPrimaryClipChangedListener method on ClipboardManager. ClipboardManager.OnPrimaryClipChangedListener has one method onPrimaryClipChanged which gets called when there is a change to primary clip.

 
private ClipboardManager.OnPrimaryClipChangedListener clipListener = new ClipboardManager.OnPrimaryClipChangedListener(){
    @Override
    public void onPrimaryClipChanged(){
        ClipData.Item item = clipboardManager.getPrimaryClip().getItemAt(0);

        CharSequence ptext = item.getText();
        pastetv.setText(ptext);
    }
};