CopyAndPaste.java by Michael Yaworski

How To Use the Class:

First things first: create an instance of the class in your activity class. For example:

public class YourActivity extends Activity {

    // declare it at the class level, but don't initialize it
    // because the instance of CopyAndPaste requires the activity Context
    private CopyAndPaste copyAndPaste;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourlayout);
		
        // after you have set your layout, initialize the instance of CopyPaste
        // with the activity Context
        copyAndPaste = new CopyAndPaste(YourActivity.this);

        // ...
    }
    // ...
}

You don't have to declare the instance at a class level; you could just create it in some method whenever you need it. However, having it like in my example is the best way to do it.

The class has four methods:

The copy method will take in a string and copy that to the Clipboard.
The copy method will take in an EditText and will paste the most recent Clip from the Clipboard to the EditText.
The pasteOption method will take in an EditText and will give the user the option to paste the most recent Clip from the Clipboard to the EditText.
The copyOrPaste method will take in an EditText and will give the user the option to paste the most recent Clip from the Clipboard to the EditText, or to copy the text from the EditText to the Clipboard.

Examples:

copyAndPaste.copy("Hello Clipboard!"); // copies the text "Hello Clipboard!" to the Clipboard

EditText et = (EditText)findViewById(R.id.editText);

copyAndPaste.paste(et); // pastes the most recent Clip on the Clipboard to the EditText

copyAndPaste.pasteOption(et); // brings up an AlertDialog to give the user the option
                              // to paste the most recent Clip on the Clipboard to the EditText
							  
copyAndPaste.copyOrPaste(et); // brings up an AlertDialog to give the user the option
                              // to paste the most recent Clip on the Clipboard to the EditText
                              // or to copy the text from the EditText to the Clipboard

These images show what the AlertDialogs for the pasteOption and copyOrPaste look like:

Now here is the actual class:

// you have to change this to your package name!
package yourPackageName;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This class uses deprecated classes for old APIs
 * and the newly introduced classes for the APIs post Honeycomb.
 *
 * @author Michael Yaworski of http://mikeyaworski.com
 * @version February 27, 2014
 */
public class CopyAndPaste {

    /**
     * The Context of the activity that this object is instantiated from
     */
    private Context context = null;

    /**
     * Constructor method.
     *
     * @param  context  the Context of the activity that this object is instantiated from
     */
    public CopyAndPaste(Context context) {
        this.context = context;
    }

    /**
     * Copies the String parameter to the Clipboard.
     * Also used as a helper method for copyOrPaste(EditText).
     *
     * @param  text  the text to be copied to the Clipboard
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public void copy(final String text) {
        try {
            if (text.length() > 0) {

                int sdk = android.os.Build.VERSION.SDK_INT;

                if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {

                    android.text.ClipboardManager clipboard = (android.text.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);

                    if (clipboard.hasText()) {
                        if (!(clipboard.getText().toString().equals(text))) { // if the last Clip is not the same as the copying text
                            clipboard.setText(text);
                            toast("Copied to clipboard.");
                        } else {
                            toast("Already on clipboard.");
                        }
                    }
                } else {
                    android.content.ClipboardManager clipboard = (android.content.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);

                    android.content.ClipData clip = android.content.ClipData.newPlainText("newDataSet", text); // creates a clip to add to the Clipboard
                    android.content.ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0); // item is the most recent Clip from the Clipboard
                    String clipText = item.coerceToText(context).toString(); // gets the Clipboard as text, no matter what type the item is.

                    if (!(clipText.equals(text))) { // if the last Clip is not the same as the copying text
                        clipboard.setPrimaryClip(clip);
                        toast("Copied to clipboard.");
                    } else {
                        toast("Already on clipboard.");
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            toast("Error!");
        }
    }

    /**
     * Returns the String representation of the most recent Clip on the Clipboard.
     * Also used as a helper method for pasteOption(EditText)
     *
     * @return the String representation of the most recent Clip on the Clipboard
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public String paste() {
        try {
            String clipText = "";

            int sdk = android.os.Build.VERSION.SDK_INT;

            if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
                android.text.ClipboardManager clipboard = (android.text.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);

                if (clipboard.hasText()) {
                    clipText = clipboard.getText().toString();
                }

            } else {
                android.content.ClipboardManager clipboard = (android.content.ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);

                android.content.ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0); // item is the most recent Clip from the Clipboard
                clipText = item.getText().toString(); // get most recent text Clip
            }
            return clipText;

        } catch (Exception exception) {
            exception.printStackTrace();
            toast("Error!");
            return null;
        }
    }

    /**
     * Pastes the String representation of the most recent Clip on the Clipboard
     * directly into the current cursor location in the EditText (parameter).
     * Also used as a helper method for copyOrPaste(EditText).
     *
     * @param  editText  the EditText that the Clipboard text should be pasted to
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public void paste(final EditText editText) {
        String clipText = paste();

        if (clipText != null) {
            final int sel = editText.getSelectionStart();
            final String originalText = editText.getText().toString();
            // insert the Clipboard text where the cursor is already and keep the cursor there
            editText.setText(originalText.substring(0, sel) + clipText + originalText.substring(sel, originalText.length()));
            editText.setSelection(sel);
        }
    }

    /**
     * Gives the option (via AlertDialog) to paste the String representation of the most recent Clip on the Clipboard
     * into the current cursor location in the EditText (parameter).
     * Uses helper method paste(EditText).
     *
     * @param  editText  the EditText that the Clipboard text should be pasted to
     */
    public void pasteOption(final EditText editText) {
        try {
            new AlertDialog.Builder(context)
                    .setCancelable(true)
                    .setNeutralButton("Paste", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            paste(editText);
                        }
                    })
                    .show();
        } catch (Exception exception) {
            exception.printStackTrace();
            toast("Error!");
        }
    }

    /**
     * Gives the option (via AlertDialog) to either copy to the Clipboard, or paste from it.
     * If the EditText is empty, there is only the paste option.
     * Uses helper methods copy(String) and paste(EditText).
     *
     * @param  editText  the EditText to copy the text from or where the Clipboard text should be pasted to
     */
    public void copyOrPaste(final EditText editText) {
        try {
            final String text = editText.getText().toString();

            if (text.length() > 0) {
                new AlertDialog.Builder(context)
                        .setCancelable(true)
                        .setNegativeButton("Copy", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                copy(text);
                            }
                        })
                        .setPositiveButton("Paste", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                paste(editText);
                            }
                        })
                        .show();
            } else {
                pasteOption(editText);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            toast("Error!");
        }
    }

    /**
     * Toasts the text from the parameter to the Context defined in the constructor.
     *
     * @param  text  the text that should be shown in the Toast
     */
    public void toast(String text) {
        Toast.makeText(context.getApplicationContext(), text, Toast.LENGTH_SHORT).show();
    }
}
DOWNLOAD

              Created: February 27, 2014
Completed in full by: Michael Yaworski