Android Copy to Clipboard Using Kotlin

Today I want to share about how you can copy text to Clipboard programmatically and paste the text from the clipboard on Android. This tutorial will be on the Kotlin language since it is very easy to learn and implement.

What the app will look like?

The app will look like this after you finish this tutorial:

Android Copy to Clipboard

What will you learn?

  • Understand how to copy text to clipboard on Android.
  • Know how to paste the text copied from the clipboard.

Note: In this post, I used Android Studio 3.5.3, make sure you use the latest Android Studio, or if you already install it, be sure to check the latest update. The Kotlin version that I used is Kotlin 1.3.61.

Getting Started РAndroid Copy to Clipboard 

Open your Android Studio and choose to Start a new Android Studio Project. Then set the Application Name CopyPasteApp and select Kotlin as the language. Give the Activity Name MainActivity and wait until the Android Studio finishes preparing your project.

Open activity_main.xml inside res/layout directory and replace the code given below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity="center"
   android:padding="20dp">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

       <EditText
           android:id="@+id/etTextToCopy"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:textSize="16sp"
           android:hint="Type something.."
           />

       <Button
           android:id="@+id/btCopyText"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginStart="12dp"
           android:text="Copy Text"
           />

   </LinearLayout>

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_marginTop="50dp">

       <TextView
           android:id="@+id/tvTextToPaste"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:textSize="16sp"
           android:hint="Paste here.."
           />

       <Button
           android:id="@+id/btPasteHere"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginStart="12dp"
           android:text="Paste Here"
           />

   </LinearLayout>

</LinearLayout>

The layout will be as simple as that. It will only consist of an EditText for you to type the text that will be copied later and a TextView to display the text from Clipboard. There are also two Buttons each to copy and paste the text.

Next, open MainActivity.kt file and proceed to the next step.

Copying Text to Clipboard

Add a new method named copyTextToClipboard().

private fun copyTextToClipboard() {
   val textToCopy = etTextToCopy.text

   val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
   val clipData = ClipData.newPlainText("text", textToCopy)
   clipboardManager.setPrimaryClip(clipData)

   Toast.makeText(this, "Text copied to clipboard", Toast.LENGTH_LONG).show()
}

The code above is for copying text from the EditText from our layout. To copy a text, you need to use a ClipboardManager alongside with ClipData instance. After that you copy the text via clipboardManagersetPrimaryClip(clipData).

Pasting Text from Clipboard

Next, add another new method named pasteTextFromClipboard().

private fun pasteTextFromClipboard() {
   val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
   tvTextToPaste.text = clipboardManager.primaryClip?.getItemAt(0)?.text
}

You can paste the text from the clipboard by accessing clipboardManager.primaryClip. Then, set the text from the clipboard to our TextView from activity_main.xml layout.

Open MainActivity.kt again, and modify the onCreate() method to add the listener for our buttons.

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

   btCopyText.setOnClickListener {
       copyTextToClipboard()
   }

   btPasteHere.setOnClickListener {
       pasteTextFromClipboard()
   }
}

The last part is to run the app and try to copy and paste the text. Yay!

Copy to Clipboard and Paste

Where to go next?

You can download this full code from the link below:

Download final code

Be sure to check my other cool posts here about:

I hope you like my post, comment and share it with love!

You may also like...

1 Response

  1. February 24, 2020

    […] Copy to Clipboard Using Kotlin on Android […]

Join the discussion...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: