Updated 15 December 2016
Everyone knows the importance of copy and paste in today’s life. Also in many scenarios you have to copy a particular text , image, link etc. in mobile devices as well. Android provide the copy and paste feature through the library of ClipboardManager and ClipData. You can use this library to copy and paste different types of data on your android devices.
For copy you have to make a instance of ClipboardManager and then put the data to be copied on ClipData object then android will put that data onto system wide Clipboard from where you can access that data anywhere in your device. So firstly lets make a ClipData object and add some random data in it like
1 2 3 4 5 6 7 8 9 10 11 |
//Instantiate ClipboardManager. ClipboardManager clipboard; clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); //Taking a dummy text in the ClipData object. ClipData copy_data; String text = "Data to be copied to clipboard"; copy_data = ClipData.newPlainText("text", text); //copy the dummy value to Clipboard. clipboard.setPrimaryClip(copy_data); |
Now your dummy text is copied to the clipboard you can add a OnLongClickListener on ant text or image then perform all these oprations inside listener. For image the URI is copied on the clipboard.
Now for pasting this dummy text or any other type of data currently in clipboard you have to again instantiate ClipboardManager and then get the data stored/ copied on the clipboard like
1 2 3 4 5 6 7 8 |
//Instantiate ClipboardManager. ClipboardManager clipboard; clipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE); //getting copied data in string format. ClipData copied_data = clipboard.getPrimaryClip(); ClipData.Item item = copied_data.getItemAt(0); String text = item.getText().toString(); |
Now you have the copied text in String type variable test you can set it anywhere you want. You can again apply OnLongClickListener on any TextView or EditText and set the copied text over there by this method.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.