How to use ClipboardManager to ClipData in Android?

In this article, we are going to use a pre-defined class ClipboardManager and learn how to use ClipData for copy and paste data in Android?
Submitted by Manu Jemini, on January 30, 2018

The example, we are going to use, we will learn about the method to use ClipboardManager and copy and paste from one EditText to another EditText with the help of two buttons. In the example, below we are going to use the Content.ClipboardManager class to have all the functionality required for ClipboardManager. This getSystemService(CLIPBOARD_SERVICE) method will create an object which can be used to process the current clipboard.

To implement this in your program you will need to import: android.content.ClipData, android.content.ClipboardManager in your java file. When our layout file is done, we will include it into the JAVA file. Now we will set EditTexts and Buttons in corresponding variables. In Java file we will have two Buttons and we will set both buttons for listeners.

For first button, we will take the text from the EditText and put it in the clipboard with a method called:

clip = ClipData.newPlainText("text", text);                
Cboard.setPrimaryClip(clip);

For the second button, we will take the text from the clipboard and set it in another EditText.

ClipData abc = Cboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);

Now we have all your functionality ready to implement a simple clipboard. We also make Toast whenever an operation happens.

1) Java file:

package com.example.hp.myapplication;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    EditText copy,paste;
    Button Button1,Button2;

     ClipboardManager Cboard;
     ClipData clip;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        copy=(EditText)findViewById(R.id.copy);
        paste=(EditText)findViewById(R.id.paste);
        Button1=(Button) findViewById(R.id.button1);
        Button2=(Button) findViewById(R.id.button2);
        Cboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        Button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String text;
                text = copy.getText().toString();

                clip = ClipData.newPlainText("text", text);
                Cboard.setPrimaryClip(clip);

                Toast.makeText(getApplicationContext(), "Your text is copied successfully", Toast.LENGTH_SHORT).show();
            }
        });

        Button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ClipData abc = Cboard.getPrimaryClip();
                ClipData.Item item = abc.getItemAt(0);

                String text = item.getText().toString();
                paste.setText(text);

                Toast.makeText(getApplicationContext(), "Your text is paste successfully", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.hp.myapplication.MainActivity">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/copy"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:hint="copy form here..." />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/paste"
        android:layout_below="@+id/copy"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:hint="paste in here..." />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to copy Text"
        android:id="@+id/button1"
        android:layout_below="@+id/paste"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to paste text"
        android:id="@+id/button2"
        android:layout_below="@+id/button1" />
</RelativeLayout>

3) Menifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output Images

Use clipboardmanager in Android 1

Use clipboardmanager in Android 2

Use clipboardmanager in Android 3

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.