Android – Copy message to clipboard

Because of deprecation issues the following code should be used to copy a text to clipboard:

if (Build.VERSION.SDK_INT < 11)
{
 android.text.ClipboardManager cm = (android.text.ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
 cm.setText(message);
 Toast.makeText(this, "Copied to clipboard", Toast.LENGTH_SHORT).show();
} else
{
 ClipboardManager cm = (ClipboardManager) this.getSystemService(Context.CLIPBOARD_SERVICE);
 ClipData clip = ClipData.newPlainText("Message text", message);
 cm.setPrimaryClip(clip);
 Toast.makeText(this, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}

Leave a Reply

You must be logged in to post a comment.