This article will show you how to copy data to android system clipboard and how to paste android system clipboard data to target text view.
1. Copy Data To System Clipboard Steps.
- Get system android.content.ClipboardManager object.
final android.content.ClipboardManager clipboardManager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
- Create a android.content.ClipData object and set source data in it
ClipData clipData = ClipData.newPlainText("Source Text", srcText);
- Set the clip data as clipboard manager’s primary clip, then the data has been copied to system clipboard.
clipboardManager.setPrimaryClip(clipData);
2. Paste Data From System Clipboards Steps.
- Get clipboard manager as in section 1.
- Get primary clip data from the clipboard manager.
ClipData clipData = clipboardManager.getPrimaryClip();
- Get clip data item and text, then set text to target textview or somewhere else.
3. Android Clipboard Management Example.
If you can not watch the above video, you can see it on the youtube URL https://youtu.be/QQ-jOK5x-9A
There are three buttons in this example. Click the first button will copy above input text to android system clipboard. Click the second button will paste the data in clipboard to below text view. Click the third button will clear the clipboard data.
4. Main Activity.
ClipBoardActivity.java
package com.dev2qa.example.clipboard; import android.content.ClipData; import android.content.ClipData.Item; import android.content.ClipboardManager; import android.os.Bundle; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.dev2qa.example.R; public class ClipBoardActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clip_board); setTitle("dev2qa.com - Android Clipboard Example."); // Get source input edittext and destination output textview. final EditText srcEditText = (EditText)findViewById(R.id.clipboard_source_edit_text); final TextView destTextView = (TextView)findViewById(R.id.clipboard_destination_text); // Get clipboard manager object. Object clipboardService = getSystemService(CLIPBOARD_SERVICE); final ClipboardManager clipboardManager = (ClipboardManager)clipboardService; // This is the copy button. Button copyButton = (Button)findViewById(R.id.clipboard_copy_button); copyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String srcText = srcEditText.getText().toString(); // Create a new ClipData. ClipData clipData = ClipData.newPlainText("Source Text", srcText); // Set it as primary clip data to copy text to system clipboard. clipboardManager.setPrimaryClip(clipData); // Popup a snackbar. Snackbar snackbar = Snackbar.make(v, "Source text has been copied to system clipboard.", Snackbar.LENGTH_LONG); snackbar.show(); } }); // This is the paste button. Button pasteButton = (Button)findViewById(R.id.clipboard_paste_button); pasteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Get clip data from clipboard. ClipData clipData = clipboardManager.getPrimaryClip(); // Get item count. int itemCount = clipData.getItemCount(); if(itemCount > 0) { // Get source text. Item item = clipData.getItemAt(0); String text = item.getText().toString(); // Set the text to target textview. destTextView.setText(text); // Show a snackbar to tell user text has been pasted. Snackbar snackbar = Snackbar.make(v, "Paste system clipboard text to target text view.", Snackbar.LENGTH_LONG); snackbar.show(); } } }); // Click this button to clear system clipboard content. Button emptyClipboardButton = (Button)findViewById(R.id.clipboard_empty_button); emptyClipboardButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ClipData clipData = ClipData.newPlainText("", ""); clipboardManager.setPrimaryClip(clipData); Toast.makeText(getApplicationContext(), "Clipboard data has been cleared.", Toast.LENGTH_LONG).show(); } }); } }
5. Activity Layout Xml File.
activity_clip_board.xml
<TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="50dp"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp"> <TextView android:layout_column="0" android:layout_weight="1" android:text="Source Text:" /> <EditText android:layout_weight="5" android:layout_span="2" android:id="@+id/clipboard_source_edit_text" android:hint="Input source text."/> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp"> <Button android:id="@+id/clipboard_copy_button" android:layout_weight="1" android:text="Copy Text" /> <Button android:id="@+id/clipboard_paste_button" android:layout_weight="1" android:text="Paste Text" /> <Button android:id="@+id/clipboard_empty_button" android:layout_weight="1" android:text="Empty Clipboard" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp"> <TextView android:layout_column="0" android:layout_weight="1" android:text="Dest Text:" /> <TextView android:layout_weight="5" android:layout_span="2" android:id="@+id/clipboard_destination_text" android:textSize="20dp"/> </TableRow> </TableLayout>