Android Clipboard Manager Example and Tutorial


Introduction :
                Android clipboard is nothing but the copy - paste functionality that we can see across any software or Operating system. This copy-paste can be done for text, images, binary stream of data or other complex data. This can simply be achieved through Android ClipboardManager with just few lines of code. In this android clipboard manager tutorial we will learn how to copy-paste text.

Tutorial :
                In order to use the clipboard, you need not instantiate this class directly instead, we can retrieve it through getSystemService().

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

Copying Data :
                Copying data can be done through instantiating Clipdata Object by calling the respective type of method. Here we need to copy text, hence we need to instantiate through newPlainText() method as shown below,

String text = copytext.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

Pasting Data :
              To get the copied data first, invoke getPrimaryClip() method and from that get the copied text from the clipboard as below,

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




Thats it.. We are done with this Android Clipboard manager...

activity_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:background="@android:color/darker_gray"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="Enter Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:onClick="copy"
        android:text="Copy" />

    <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="18dp"
        android:onClick="paste"
        android:text="Paste" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:ems="10" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText2"
        android:layout_below="@+id/textView1"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>

MainActivity.java


import android.annotation.SuppressLint;
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 MainActivity extends Activity {

 private EditText copytext, pastetext;
 private ClipboardManager myClipboard;
 private ClipData myClip;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
  copytext = (EditText) findViewById(R.id.editText1);
  pastetext = (EditText) findViewById(R.id.editText2);

 }

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

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

}

Source code of this application
AndroidClipboard.zip



Reactions:

1 comment :

  1. thanks for the helpful article
    how can I know when clipboard's value will be change?

    ReplyDelete