Hi Guys, Welcome to Proto Coders Point, In this Android Tutorial will learn how to copy a text programmatically into clipboard in android apps, Source Code below for both JAVA & KOTLIN developer.
Copy text to clipboard android programmatically
Below is complete source code to learn how to copy and paste on android programmatically (copy to clipboard android).
To Implement the same, we will use android clipboard manager
clipboard manager in android
Basically, android clipboard manager is a interface to a clipboard services, use to easily save any text in clipData and to retrieve the text global in a mobile device.
The ClipboardManager is best, easily to use & understand: it contains some methods by which we can get & set the current clipboard data as primary copyed text.
Snippet Code For Java
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clipData = ClipData.newPlainText("label","The Text you want to copy in clipboard"); // Example: In real-time, Value from textview clipboardManager.setPrimaryClip(clipData);
Snippet Code For Kotlin
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager val clipData = ClipData.newPlainText("label", txtview2cpy!!.text.toString()) clipboardManager.setPrimaryClip(clipData)
Clipboard manager android example with source code
Note: Below code is just an example, Use it to implement in real-time projects. ALL THE BEST.
Code Explaination in brief
Below I have 2 views (TextView & a Button). In TextView I have a text that I want to copy into android clipboard as PainText data, Then I have a button that simply copy the text from textview and get saved into clipdata (so that I can paste the copied text somewhere else easily).
activity_main.xml – UI Design
<?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/txtview2cpy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="32dp" android:textAlignment="center" android:text="On Click, This TextView Text get Copyed in clipboard programmatically...!!" android:textSize="22dp" app:layout_constraintBottom_toTopOf="@+id/bCopyText" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> <Button android:id="@+id/bCopyText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Copy" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt – For Kotlin Developer
package example.on.androidtextcopy import androidx.appcompat.app.AppCompatActivity import android.widget.TextView import android.os.Bundle import example.on.androidtextcopy.R import android.content.ClipData import android.content.ClipboardManager import android.view.View import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() { var txtview2cpy: TextView? = null var bCopyText: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) txtview2cpy = findViewById<View>(R.id.txtview2cpy) as TextView bCopyText = findViewById<View>(R.id.bCopyText) as Button val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager bCopyText!!.setOnClickListener { val clipData = ClipData.newPlainText("label", txtview2cpy!!.text.toString()) clipboardManager.setPrimaryClip(clipData) Toast.makeText(this@MainActivity, "Text Copyed", Toast.LENGTH_SHORT).show() } } }
MainActivity.java – copy to clipboard android programmatically
package example.on.androidtextcopy; import androidx.appcompat.app.AppCompatActivity; import android.content.ClipData; import android.content.ClipboardManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { TextView txtview2cpy; Button bCopyText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtview2cpy = (TextView) findViewById(R.id.txtview2cpy); bCopyText = (Button) findViewById(R.id.bCopyText); ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); bCopyText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ClipData clipData = ClipData.newPlainText("label",txtview2cpy.getText().toString()); clipboardManager.setPrimaryClip(clipData); Toast.makeText(MainActivity.this,"Text Copyed",Toast.LENGTH_SHORT).show(); } }); } }