Copy text to clipboard – Android Programming

clipboard_android
 

Copy text to clipboard in Android

In this article, we are going to learn how to copy texts to the clipboard in Android. We are going to make a simple android project, which will have a TextView with some texts in it and an EditText where you can paste the copied text. Now tapping on the TextView, the text will be copied to the Clipboard.

You can also check my app Grab Deals on play store where I have used this code to copy coupon codes to the clipboard.

I am making this project as simple as possible. So the layout will contain only a TextView with no other designs or feature in it and an EditText where we will paste the copied text.

clipboard_android

So let’s get started.

Let’s make a project. Name your project. I am naming it Clipboard Example. We don’t need any special permission for this project, so you can leave the manifest file as it is.

Go to your activity_main.xml file and create a TextView and an EditText. Now our activity_main.xml looks like this.

After creating the layout, go to your MainActivity.java file and paste the code below.

Now run the project and tap the TextView. The texts in it will be copied to the clipboard. You can now paste the copied text in the EditText field. Tap and hold the EditText field until the PASTE option appears.

 

 

You May Also Like

2 Comments

  1. Thanks for the tutorial Rupam. But I did not understand what is the first parameter of the method “ClipData.newPlainText(“My Text”,text);”?

Leave a Reply

Your email address will not be published. Required fields are marked *