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 type 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.

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.

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

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