Open In App

Clipboard in Android

Last Updated : 20 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Android’s Clipboard performs copying and pasting on different data types, such as text strings, images, binary stream data, and other complex data types. Clipboard does the copying and pasting operations within the same application and between multiple applications that have implemented the clipboard framework. Clipboard has a limitation on the number of clip objects it can hold at a time. The clipboard can hold only one object at a time. If an object is put on the clipboard, the previously held object on the clipboard is dropped. The clip object can take in three types of data:

  • Text: A string can directly be put into the clip object and then into the clipboard. We can then get the clip object from the clipboard and paste the string into the application’s text or storage fields.
  • URI: It is used for copying complex data from the content provider. A URI object can be put into a clip object and then loaded onto the clipboard. To perform a paste operation, the clip object must be resolved into the source, such as a content provider.
  • Intent: An intent object must be created and put into a clip object and loaded onto the clipboard. Paste action, similar to the text, can be performed.

Step by Step Implementation

To make an application that stores some data into the clipboard and derives the data from it in Android, we follow the following steps:

Saving to Clipboard:

A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Clipboard in Android Sample GIF

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Go to the activity_main.xml file which represents the UI of the application. Create an EditText where we shall supply the text to be saved in the clipboard, and a Button to perform the saving action. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  
    <!--Text must be entered here-->
    <EditText
        android:id="@+id/txtCopy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnCopy"
        android:layout_centerHorizontal="true"
        android:hint="Type something..." />
  
    <!--Text entered in the above field gets copied to 
        Clipboard on this button click-->
    <Button
        android:id="@+id/btnCopy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Copy to Clipboard" />
  
</RelativeLayout>


Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
          
        // Declaring the edit text and button from the layout file
        val copyTxt = findViewById<EditText>(R.id.txtCopy)
        val copyBtn = findViewById<Button>(R.id.btnCopy)
  
        // Initializing the ClipboardManager and Clip data
        val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        var clipData: ClipData
  
        // Action when the copy button is clicked
        copyBtn.setOnClickListener {
  
            // Text from the edit text is stored in a val
            val txtCopy = copyTxt!!.text.toString()
  
            // clip data is initialized with the text variable declared above
            clipData = ClipData.newPlainText("text", txtCopy)
  
            // Clipboard saves this clip object
            clipboardManager.setPrimaryClip(clipData)
  
            // A toast is shown for user reference that the text is copied to the clipboard
            Toast.makeText(applicationContext, "Copied to Clipboard", Toast.LENGTH_SHORT).show()
        }
    }
}


Output: Run on Emulator

Pasting from Clipboard:

A sample GIF is given below to get an idea about what we are going to do in this section.

Clipboard in Android Sample GIF

Step 1: Working with the activity_main.xml file

Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  
    <!--Text from the clip object will be shown here*-->
    <TextView
        android:id="@+id/txtShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnShow"
        android:layout_centerHorizontal="true"
        android:hint="Clipboard Data" />
  
    <!--*on this button click-->
    <Button
        android:id="@+id/btnShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Clipboard Data" />
  
</RelativeLayout>


Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.content.ClipboardManager
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Declare the textview and button from the layout file
        val pasteTxt = findViewById<TextView>(R.id.txtShow)
        val btnPaste = findViewById<Button>(R.id.btnShow)
  
        // Declaring the clipboard manager
        val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
  
        // Action on paste button click
        btnPaste.setOnClickListener {
  
            // Storing the clip data in a variable
            val pData = clipboardManager.primaryClip
  
            // Retrieving the items
            val item = pData!!.getItemAt(0)
  
            // item is converted to string and stored in a variable
            val txtPaste = item.text.toString()
  
            // Textview is set as txtPaste string
            pasteTxt!!.text = txtPaste
  
            // Toast for user reference
            Toast.makeText(applicationContext, "Pasted from Clipboard", Toast.LENGTH_SHORT).show()
        }
    }
}


Output: Run on Emulator



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads