How To Copy Text From EditText In Kotlin - Android Code Snippets

Posted on Thursday, November 1, 2018

To make your Android app easy to use with more features and functionality, you should add a button to copy the EditText text automatically! I made a code tutorial about the java code to copy to clipboard in that topic here.

So here is the Kotlin code to copy text from EditText to system clipboard. Open you Android Studio, create a new project or use an existing one 😂 then make a button or ImageButton (as you like) to use it as a copy text button, and choose a certain EditText View to copy the text content of it.

In modern Kotlin implementation in Android Studio you don't need to initialize the View of the layout (unlike the case in Java). So dive into code!

First, add the onClick to the Button or ImageButton layout XML file like this.
<Button
android:onClick="copyText"
android:text="Copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/copyBtn"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.302"/>

then create the copyText function in the Kotlin class, like this.
fun copyText(v: View){
     copy2clipboard(et2Copy.text.toString())
    }
When the button clicked, the copyText function fired, then the copy2clipboard function fired to copy the et2Copy EditText. Here is the code of copy2clipboard function.
fun copy2clipboard(text: CharSequence){
        val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clip = ClipData.newPlainText("copy text", text)
        clipboard.primaryClip = clip
    }
Thus, the copy2clipboard will copy any text passed to it as an argument (args). So in the function of out copy button, we fired this "copy" function with the argument text to copy. the text to copy is the text contained in the EditText which has an ID equals et2Copy as you can see on the complete XML layout here.
<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/et2Copy"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintVertical_bias="0.017"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/et2Paste"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintVertical_bias="0.153"/>

<Button
android:onClick="copyText"
android:text="Copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/copyBtn"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.302"/>

</android.support.constraint.ConstraintLayout>
And we did it ✌ I hope you enjoyed this to-target code tutorial, and see you in the next code snippet here! 

No comments:

Post a Comment

© Copyrights Received