Android & Kotlin

Android TextView enable Copy Paste

Pinterest LinkedIn Tumblr

In this post, I will show you how to implement android TextView enable copy paste. For doing that we’ll create a sample app and Implement copy/paste in your TextView in Android programmatically.

Android EditText has inbuilt functionality whereby you can copy text through a long press selected text but in TextView you have to write some code for that. Let’s get started

1. Register View for ContextMenu

One activity that has an inbuilt method is registerForContextMenu(View view). Using this method we’ll register view for the pop-menu. In this article, we’re making a simple app. So let’s open activity layout file and add TextView and EditText

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi Welcome All "
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text=" "
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

For demonstration, we’ll copy text from TextView and paste in EditText. Now let register TextView for ContextMenu

2. In Activity, on create method add below line of code

In activity onCreate() method you have to initialize TextView and call registerForContextMenu() method and pass your TextView instance on it .

registerForContextMenu(textView)

According to an android official, the doc registerForContextMenu() used to register a context menu to be shown for the given view. In this sample, we are registered TextView.

3. Now to override a method onCreateContextMenu()

Now you have to override a method onCreateContextMenu(), which will help us achieve our copy functionality.

  override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        menu?.add(0, v!!.id, 0, "Copy")
        //setting header title for menu
        menu?.setHeaderTitle("Copy Selected Text")

        var manager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val textView = v as TextView
        val clipData = ClipData.newPlainText("text", textView.text)
        manager.setPrimaryClip(clipData)
        super.onCreateContextMenu(menu, v, menuInfo)

    }
Finally, your full source code seems like below
 package com.textviewcopypaste

import android.content.ClipData
import android.content.ClipboardManager
import android.os.Bundle
import android.view.ContextMenu
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        registerForContextMenu(textView)
    }

    override fun onCreateContextMenu(
        menu: ContextMenu?,
        v: View?,
        menuInfo: ContextMenu.ContextMenuInfo?
    ) {
        menu?.add(0, v!!.id, 0, "Copy")
        //setting header title for menu
        menu?.setHeaderTitle("Copy Selected Text")

        var manager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        val textView = v as TextView
        val clipData = ClipData.newPlainText("text", textView.text)
        manager.setPrimaryClip(clipData)
        super.onCreateContextMenu(menu, v, menuInfo)

    }
}

Conclusion

Wow! Great job! You just finished the Implement copy/paste in your TextView in Android. If you have any queries, feel free to connect us.

Get Solution Code

Best way of logging and debugging your android application

Write A Comment