Can anybody please tell me how to copy the text present in a particular textview to clipboard when a button is pressed?
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainpage); textView = (TextView) findViewById(R.id.textview); copyText = (Button) findViewById(R.id.bCopy); copyText.setOnClickListener(new View.OnClickListener() { @Override 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 in TextView textView to clipboard when the Button bCopy
is pressed.
Select the text or graphics you want to copy, and press Ctrl+C. Each selection appears in the Clipboard, with the latest at the top.
Open the messaging app on your Android, and press the + symbol to the left of the text field. Select the keyboard icon. When the keyboard appears, select the > symbol at the top. Here, you can tap the clipboard icon to open the Android clipboard.
Long-press on a word and tap “Copy” or take a screenshot to copy something to your Android clipboard. Your copied text or image won't be stored in the clipboard forever, though. If you don't paste it immediately, it will be erased from the memory after a short while or when you copy another piece of text.
use ClipboardManager
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText(label, text); clipboard.setPrimaryClip(clip);
make sure you have imported android.content.ClipboardManager
and NOT android.text.ClipboardManager
. Latter is deprecated. Check this link for Further information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With