Clipboard APIs in Android

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 , you need to instantiate ClipboardManager object by calling the getSystemService() method as given below:

 

ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

 

How to Copy data
After creating ClipboardManager object you need 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);
myClipboard.setPrimaryClip(myClip);

 

How to Paste data
To paste the data, we need to 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 as below:

 

ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();

 

 

Besides these methods , there are other methods provided by the ClipboardManager class for managing clipboard framework which can be directly used to get the copied text from the clipboard.

Below is an example explaining 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.

Steps:

1.) Create a new project by File-> New -> Android Project name it AndroidClipboard.

2.) Write following into main.xml:

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="19dp"
android:text="@string/copytext"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="20dp"
android:ems="10" >

<requestFocus />
</EditText>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_centerVertical="true"
android:text="@string/pastetext"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="65dp"
android:layout_marginTop="20dp"
android:onClick="copy"
android:text="@string/copy" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="39dp"
android:ems="10" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/editText2"
android:layout_marginTop="34dp"
android:onClick="paste"
android:text="@string/paste" />

</RelativeLayout>

 

 

3.) Create and write following into strings.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">AndroidClipboard</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="copy">Copy Text</string>
<string name="paste">Paste Text</string>
<string name="copytext">Text to copy</string>
<string name="pastetext">Copied Text</string>
</resources>

 

4.) Write following into main activity class:

 

package com.example.androidclipboard;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.os.Bundle;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class AndroidClipboardActivity extends Activity {

private ClipboardManager myClipboard;
private ClipData myClip;
private EditText copyField,pasteField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
copyField = (EditText)findViewById(R.id.editText1);
pasteField = (EditText)findViewById(R.id.editText2);

}

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

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

 

 

OutPut

clipboard2

clipboard1