How do i copy text programmatically in Android using kotlin code?

Published April 24, 2021

In this Android programming example i will show you how to copy text programmatically using kotlin code. To copy text in android we will use CLIPBOARD_SERVICE which will return Clipboard Manager object.

 

Let's get started

Step 1: Create an android application

Step 2: Update xml file with below code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:text="Copy Text Programmatically"
        android:textAlignment="center"
        android:textColor="@color/teal_700"
        android:textSize="32sp"
        android:textStyle="bold" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:hint="Enter your text" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="copyText"
        android:text="Copy text" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="pasteText"
        android:text="Paste text" />
    <TextView
        android:textColor="@color/red"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="24sp"
        android:textStyle="bold" />
</LinearLayout>

 

Step 3: To copy text programmatically we will pass ClipData to clipboardManager.setPrimaryClip() method.

Update activity code with below

package com.rrtutors.kotlincource
import android.content.ClipData
import android.content.ClipboardManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.content.Context
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast

class CopyTextActivity : AppCompatActivity() {
    lateinit var editText: EditText
    lateinit var textView: TextView
    lateinit var clipboardManager: ClipboardManager
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_copy_text)
        title = "KotlinApp"
        editText = findViewById(R.id.editText)
        textView = findViewById(R.id.textView)
    }

    fun copyText(view: View) {
        val text = editText.text.toString()
        if (text.isNotEmpty()) {
            clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val clipData = ClipData.newPlainText("key", text)
            clipboardManager.setPrimaryClip(clipData)
            Toast.makeText(applicationContext, "Copied", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(applicationContext, "No text to be copied", Toast.LENGTH_SHORT).show()
        }
    }
    fun pasteText(view: View) {
        val clipData: ClipData = clipboardManager.primaryClip!!
        val item: ClipData.Item = clipData.getItemAt(0)
        textView.text = item.text.toString()
        Toast.makeText(applicationContext, "Text is being Pasted", Toast.LENGTH_SHORT).show()

    }
}

 

Step 4: Run Application and press on button, now it will read the text from EditText and copy to the clipboard data.

 

 

Copy text programmatically android using kotlin code

 

Conclusion: In this android example we covered how to copy text programmatically using clipboard manager and paste the copied text.

 

How do i read text file in android using kotlin code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1565 Views