Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
Clipboard in Android
Next article icon

Clipboard in Android

Last Updated : 20 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
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      xmlns:android="http://schemas.android.com/apk/res/android"     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      xmlns:android="http://schemas.android.com/apk/res/android"     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


Next Article
Clipboard in Android

A

aashaypawar
Improve
Article Tags :
  • Kotlin
  • Android
  • Android-View

Similar Reads

    Android - Clipboard Manager with Example
    Clipboard Manager performs copy-and-paste operations within android applications. With the help of this user is able to copy and paste data across the different applications in android. In this article, we will look at How to implement Clipboard Manager in Android 13. A sample video is given below t
    3 min read
    Flip Card Animation in Android
    In this article, we are going to see how to build a Flip Card Animation app in Android Studio. Animation makes our app more attractive, convincing, and user-friendly. A sample GIF is given below to get an idea about what we are going to do in this article. Please note that we will be using Kotlin as
    4 min read
    PopView in Android
    In this article, PopView is added to android. When users tap on the view a pop animation with a circular dust effect will appear. A new view can also appear after the popping of the older view. PopView can be used to hide the view or change the view. It makes the user interface more attractive. Supp
    3 min read
    Number Picker in Android
    There are many Android Applications in our phones that we use in our day-to-day life, and many of them use a scrollable list of items to select the quantity of something. This scrollable list of numbers can be shown by a number picker.  With the help of Number Picker, users can experience a smooth a
    3 min read
    Android Jetpack Compose: Use Clipboard Service
    Many times in the android application we want to provide an easy functionality within our android application so that users can simply copy the text from the TextView or text field to the clipboard and we can paste that text into another application of google search bar. In this article, we will tak
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences