Android Clipboard Tutorial - Android Programming tutorial With Examples

Android provides the clipboard framework for copying and pasting different types of data. The data could be text , images, binary stream data or other complex data types.
Android provides the library of ClipboardManager and ClipData and ClipData.item to use the copying and pasting framework.In order to use clipboard framework, you need to put data into clip object, and then put that object into system wide clipboard.
In order to use clipboard , you need to instantiate an object of ClipboardManager by calling thegetSystemService() method. Its syntax is given below:
ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

Copying data

The next thing you need to do is to instantiate the ClipData object by calling the respective type of data method of the ClipData class. In case of text data , the newPlainText method will be called. After that you have to set that data as the clip of the Clipbaoard Manager object.Its syntax is given below:
ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
The ClipData object can take these three form and following functions are used to create those forms.
Sr.NoClipData Form & Method
newPlainText(label, text)
Returns a ClipData object whose single ClipData.Item object contains a text string.
newUri(resolver, label, URI)
Returns a ClipData object whose single ClipData.Item object contains a URI. 
newIntent(label, intent)
Returns a ClipData object whose single ClipData.Item object contains an Intent.

Pasting data

In order to paste the data, we will first get the clip by calling the getPrimaryClip() method. And from that click we will get the item in ClipData.Item object. And from the object we will get the data. Its syntax is given below:
ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();
Apart from the these methods , there are other methods provided by the ClipboardManager class for managing clipboard framework. These methods are listed below:
Sr.NoMethod & description
This method just returns the current primary clip on the clipboard
This method returns a description of the current primary clip on the clipboard but not a copy of its data.
This method returns true if there is currently a primary clip on the clipboard
4setPrimaryClip(ClipData clip)
This method sets the current primary clip on the clipboard
5setText(CharSequence text)
This method can be directly used to copy text into the clipboard
This method can be directly used to get the copied text from the clipboard


Here is an example demonstrating the use of ClipboardManager class. It creates a basic copy paste application that allows you to copy the text and then paste it via clipboard.
To experiment with this example , you can run this on an actual device or in an emulator.
1You will use Eclipse IDE to create an Android application and name it as Clipboard under a package com.example.clipboard. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/ file to add necessary code.
3Modify the res/layout/activity_main to add respective XML components
4Modify the res/values/string.xml to add necessary string components
5Run the application and choose a running android device and install the application on it and verify the results
Following is the content of the modifed main activity file src/com.example.clipboard/
package com.example.clipboard;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

   private ClipboardManager myClipboard;
   private ClipData myClip;
   private EditText copyField,pasteField;
   protected void onCreate(Bundle savedInstanceState) {
      myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
      copyField = (EditText)findViewById(;
      pasteField = (EditText)findViewById(;


   public void copy(View view){
      String text = copyField.getText().toString();
      myClip = ClipData.newPlainText("text", text);
      Toast.makeText(getApplicationContext(), "Text Copied", 

   public void paste(View view){
      ClipData abc = myClipboard.getPrimaryClip();
      ClipData.Item item = abc.getItemAt(0);
      String text = item.getText().toString();
      Toast.makeText(getApplicationContext(), "Text Pasted", 

   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(, menu);
      return true;

Following is the modified content of the xml res/layout/activity_main.xml.





Following is the content of the res/values/string.xml.

   Hello world!
   Copy Text
   Paste Text
   Text to copy
   Copied Text

Following is the content of AndroidManifest.xml file.




Let's try to run our Clipboard application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window:
Now just enter any text in the Text to copy field and then select the copy text button. The following notification will be displayed which is shown below:
Now just press the paste button, and you will see the text which is copied is now pasted in the field of Copied Text. It is shown below:

No comments

Please don't spam, comment your view.

Web Development