Skip to content

Android Clipboard – Copy and Paste Text Example in Kotlin

Android Clipboard Text – Copy and Paste Android: Android provides a powerful clipboard-based framework for copying and pasting. It supports both simple and complex data types, including text strings, complex data structures, text and binary stream data, and even application assets. 

Simple text data is stored directly in the clipboard and complex data is stored as a reference that the pasting application resolves with a content provider. Copying and pasting work both within an application and between applications that implement the framework.

Android Clipboard - Copy and Paste Text Example in Kotlin

In this tutorial, you will learn the following:

  1. Overview of Android Clipboard
  2. An Example of Android Clipboard – Copy and Paste Text in kotlin.

Android Clipboard Framework

The clip object can take one of three forms:

Text

newPlainText(label, text)

Returns a ClipData object whose single ClipData. Item object contains a text string.

URI

newUri(resolver, label, URI)

Returns a ClipData object whose single ClipData.Item object contains a URI.

Intent

newIntent(label, intent)

Returns a ClipData object whose single ClipData. Item object contains an Intent.

Some Important Points about Android clipboard

  • The clipboard holds only one clip object at a time. When an application puts a clip object on the clipboard, the previous clip object disappears.
  • If You want to allow paste data in your app, then you don’t need to handle all types of data. You can example the clipboard data, before giving an option to app users.
  • Clip object also contains Metadata, which has what MIME type or types are available. This Metadata helps you handle data.
  • If you have an application that primarily handles text, you may want to ignore clip objects that contain a URI or Intent.

Let’s Build Android Clipboard – Copy and Paste Text Example in Kotlin

In this tutorial, you will learn How to store simple text data in Clipboard and get the value.

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. In res layout file “activity_main.xml”  – Add following code in the layout file

In the User interface using 2 Buttons – One button for copy, the text from EditText and another one paste the test in TextView.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et_copy_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:ems="10"
        android:inputType="text"
        android:hint="Name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/tv_text_paste"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_copy_text" />

    <Button
        android:id="@+id/btn_copy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="Copy"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_text_paste"
        android:onClick="copyText"/>

    <Button
        android:id="@+id/btn_paste"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="16dp"
        android:text="Paste"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_text_paste"
        android:onClick="pasteText"/>

</android.support.constraint.ConstraintLayout>
Step 3. Open the “MainActivity.kt” and add following code

Handling the action on click button methods which mentioned in the XML file and showing action information in TOAST.

package `in`.eyehunt.clipboardcopypastetext

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.content.ClipData
import android.content.ClipboardManager
import android.view.View
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    private var myClipboard: ClipboardManager? = null
    private var myClip: ClipData? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        myClipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager?;

    }

    // on click copy button
    fun copyText(view: View) {
        myClip = ClipData.newPlainText("text", et_copy_text.text);
        myClipboard?.setPrimaryClip(myClip);

        Toast.makeText(this, "Text Copied", Toast.LENGTH_SHORT).show();
    }

    // on click paste button
    fun pasteText(view: View) {
        val abc = myClipboard?.getPrimaryClip()
        val item = abc?.getItemAt(0)

        tv_text_paste.text = item?.text.toString()

        Toast.makeText(applicationContext, "Text Pasted", Toast.LENGTH_SHORT).show()
    }
}
Step 4. Now Run the application, in an emulator or On your Android device

Output screenshot Android Clipboard – Copy and Paste Text

Download source code Android Clipboard Text- Copy and Paste

https://github.com/EyeHunts/ClipboardCopyPasteText

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin

4 thoughts on “Android Clipboard – Copy and Paste Text Example in Kotlin”

  1. This is exactly the thing I was looking for, thank you–it’s right at the level that I needed. One question:
    this line:
    myClipboard?.setPrimaryClip(myClip);

    keeps giving me an error:
    Smart cast to ‘ClipData!’ is impossible, because ‘myClip’ is a mutable property that could have been changed by this time

    I’m new to Kotlin, so I’m curious why this error doesn’t happen for you, and what you think I might do to fix it?
    Thanks!

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading