Clipboard in Android
Last Updated :
20 Nov, 2021
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.
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" >
< 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..." />
< 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)
val copyTxt = findViewById<EditText>(R.id.txtCopy)
val copyBtn = findViewById<Button>(R.id.btnCopy)
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
var clipData: ClipData
copyBtn.setOnClickListener {
val txtCopy = copyTxt!!.text.toString()
clipData = ClipData.newPlainText( "text" , txtCopy)
clipboardManager.setPrimaryClip(clipData)
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.
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" >
< 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" />
< 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)
val pasteTxt = findViewById<TextView>(R.id.txtShow)
val btnPaste = findViewById<Button>(R.id.btnShow)
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
btnPaste.setOnClickListener {
val pData = clipboardManager.primaryClip
val item = pData!!.getItemAt( 0 )
val txtPaste = item.text.toString()
pasteTxt!!.text = txtPaste
Toast.makeText(applicationContext, "Pasted from Clipboard" , Toast.LENGTH_SHORT).show()
}
}
}
|
Output: Run on Emulator
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...