Darshan Gowda Darshan Gowda - 1 year ago 96
Android Question

How to Copy Text to Clip Board in Android?

Can anybody please tell me how to copy the text present in a particular textview to clipboard when a button is pressed?

protected void onCreate(Bundle savedInstanceState) {
textView = (TextView) findViewById(R.id.textview);
copyText = (Button) findViewById(R.id.bCopy);
copyText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String getstring = textView.getText().toString();

//Help to continue :)

I want to copy the text of textView to clipboard when the button bCopy is pressed. Please help me. Thanks.

Answer Source

use ClipboardManager

 ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
 ClipData clip = ClipData.newPlainText(label, text);

make sure you have imported android.content.ClipboardManager and NOT android.text.ClipboardManager. Latter is deprecated. Check this link for Further information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download