Android – Copy message to clipboard

Do you want to copy some string to clipboard? Use this.

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(); } [/code]

Related Posts

Leave a reply