May 30, 2011

Android Clipboard Framework

Android SDK 3.0 has various new API. Android 3.0 has 1180 new classes, methods and interfaces. (Click Here for complete list).  Clipboard Framework is newly introduce in Android 3.0. basic purpose of this framework is to provide copy and paste data between various application.  It is very powerful framework. It supports simple data like text (String) and complex type like binary data, objects etc.

Here I am going to explain very simple example of Android Clipboard Framework. Let's start with introduction of key classes.

1. ClipboardManager: It is represent as global object. We do not need to create new object of ClipboardManager. We can directly fetch object from system service. we can get reference by invoking getSystemService(CLIPBOARD_SERVICE).

2. ClipData: To put any object in clipboard we need to create object of ClipData class. ClipData object contain ClipData.Item and ClipDescription.

3. ClipData.Item: It contain object which we copied. Object can be Text, URI or Intent data.

4. ClipDescription: It is metadata about clip. It contain MIME type of clip data.
Let's start with very simple demo of ClipBoard Framework.
Step 1: Create Android Project with following settings

Project Name : CopyAndPasteDemo
Package Name : com.kpbird.cp
Activity Name : CopyAndPasteDemo
Android SDK : 3.0

Step 2: Open main.xml, we are going to create layout first. place two edit text component and two buttons for copy and paste. We will use one edit text as source and another edit text as destination. when user press copy button we will copy content of first edit text. when user press paste button we will paste content in second edit test.


Step 3 : Open and write following coding.
Here we have created object of ClipboradManager, Buttons and EditText. we are fetching object of ClipboardManager from getSystemService in onCreate method.
We have implement OnClickListener event for buttons.

When user click on Copy button. we need to create object of ClipData and set the Text in it. Following code is used to copy text

ClipData cd1 = ClipData.newPlainText("MyData", txtCopy.getText());

Following code is used to paste value

ClipData cd2 = cm.getPrimaryClip();

Full Source Code.

package com.kpbird.cp;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CopyAndPasteDemo extends Activity implements OnClickListener {

 private ClipboardManager cm;
 private Button btnCopy;
 private Button btnPaste;
 private EditText txtCopy;
 private EditText txtPaste;
    public void onCreate(Bundle savedInstanceState) {
        cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        btnCopy = (Button) findViewById(;
        btnPaste = (Button) findViewById(;
        txtCopy = (EditText) findViewById(;
        txtPaste = (EditText) findViewById(;

 public void onClick(View v) {
   ClipData cd1 = ClipData.newPlainText("MyData", txtCopy.getText());
   Toast t = Toast.makeText(this, "Text Copied", Toast.LENGTH_SHORT);;
   ClipData cd2 = cm.getPrimaryClip();
   Toast t1 = Toast.makeText(this, "Text Pested", Toast.LENGTH_SHORT);;