Android

Android Clipboard

Android Clipboard

In Android, you will get the clipboard functionality used to copy and paste various kinds of data. You can implement the Clipboard functionality on Android with the help of a library ClipboardManager and ClipData.item. The example below will demonstrate the use of ClipboardManager class for your android application. The steps you need to follow to implement the Clipboard application include:

1.You will need to create an application under a package – com.firstapp.ashulakhwan.greatlearning

2.Next, you need to modify the file named MainActivity.java with the following code:

package com.firstapp.ashulakhwan.greatlearning;

package com.firstapp.ashulakhwan.greatlearning;

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

import android.support.v8.app.ActionBarActivity;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
   EditText edit1, edit2;
   Button btn1, btn2;

   private ClipboardManager firstClipboard;
   private ClipData firstClip;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      edit1 = (EditText) findViewById(R.id.editText);
      edit2 = (EditText) findViewById(R.id.editText2);

      btn1 = (Button) findViewById(R.id.button);
      btn2 = (Button) findViewById(R.id.button2);

      firstClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

      btn1.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            String text;
            text = edit1.getText().toString();

            firstClip = ClipData.newPlainText("text", text);
            firstClipboard.setPrimaryClip(firstClip);

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

      btn2.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View v) {
            ClipData txt = firstClipboard.getPrimaryClip();
            ClipData.Item item = txt.getItemAt(0);

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

            Toast.makeText(getApplicationContext(), "The copied text is pasted.", 
               Toast.LENGTH_SHORT).show();
         }
      });
   }

}

3.After that, you are required to modify the XML layout file which is named activity_main.xml and the content is shown below:

<?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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Great Learning"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#d5d630"
      android:textSize="40dp" />
   
   <TextView android:text="ClipboardExample" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="40dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Copy the text"
      android:layout_below="@+id/imageView"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_alignLeft="@+id/editText"
      android:layout_alignStart="@+id/editText"
      android:hint="paste the copied text"
      android:layout_below="@+id/editText"
      android:layout_alignRight="@+id/editText"
      android:layout_alignEnd="@+id/editText" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Copy"
      android:id="@+id/button"
      android:layout_below="@+id/editText2"
      android:layout_alignLeft="@+id/editText2"
      android:layout_alignStart="@+id/editText2" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Paste"
      android:id="@+id/button2"
      android:layout_below="@+id/editText2"
      android:layout_alignRight="@+id/editText2"
      android:layout_alignEnd="@+id/editText2" />
      
</RelativeLayout>

4. In the next step, you will need to modify the AndroidManifest.xml with the code shown below:
 

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

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.firstapp.ashulakhwan.greatlearning.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
   </application>

</manifest>

At last, you can run the application and check the results of your application. 

Top course recommendations for you

    Arduino vs Raspberry Pi
    2 hrs
    Beginner
    7.3K+ Learners
    4.55  (616)
    Joins in SQL
    2 hrs
    Beginner
    10.6K+ Learners
    4.42  (603)
    Design Thinking for Beginners
    1 hrs
    Beginner
    14.8K+ Learners
    4.4  (1513)
    Database Management System
    1 hrs
    Beginner
    41.4K+ Learners
    4.39  (5258)
    Become a Web Developer
    1 hrs
    Beginner
    17.8K+ Learners
    4.4  (352)
    .NET Fundamentals
    2 hrs
    Beginner
    24.1K+ Learners
    4.41  (1907)