Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

@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.

like image 553
Darshan Gowda Avatar asked Oct 08 '13 16:10

Darshan Gowda


People also ask

How do I copy text to the clipboard?

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.

How do you use clipboard on Android?

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.

How do I copy files to clipboard on Android?

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.


1 Answers

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.

like image 94
stinepike Avatar answered Oct 07 '22 02:10

stinepike