Android provides the clipboard framework for copying and pasting different types of data. The data could be text, images, binary stream data or other complex data types.
Android provides the library of ClipboardManager and ClipData and ClipData.item to use the copying and pasting framework.In order to use clipboard framework, you need to put data into clip object, and then put that object into system wide clipboard.
In order to use clipboard , you need to instantiate an object of ClipboardManager by calling the getSystemService() method. Its syntax is given below −
1 |
ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); |
The next thing you need to do is to instantiate the ClipData object by calling the respective type of data method of the ClipData class. In case of text data , the newPlainText method will be called. After that you have to set that data as the clip of the Clipboard Manager object.Its syntax is given below −
1 2 |
ClipData clip = ClipData.newPlainText("clipboard data ", your_text); clipboard.setPrimaryClip(clip); |
The ClipData object can take these three form and following functions are used to create those forms.
ClipData Form | Method & Description |
---|---|
Text | newPlainText(label, text) Returns a ClipData object whose single ClipData.Item object contains a text string. |
URI | newUri(resolver, label, URI) Returns a ClipData object whose single ClipData.Item object contains a URI. |
Intent | newIntent(label, intent) Returns a ClipData object whose single ClipData.Item object contains an Intent. |
In order to paste the data, we will first get the clip by calling the getPrimaryClip() method. And from that click we will get the item in ClipData.Item object. And from the object we will get the data. Its syntax is given below −
1 2 3 |
ClipData abc = clipboard.getPrimaryClip(); ClipData.Item item = abc.getItemAt(0); String text = item.getText().toString(); |
Apart from the these methods , there are other methods provided by the ClipboardManager class for managing clipboard framework. These methods are listed below −
Method | Description |
---|---|
getPrimaryClip() | This method just returns the current primary clip on the clipboard. |
getPrimaryClipDescription() | This method returns a description of the current primary clip on the clipboard but not a copy of its data. |
hasPrimaryClip() | This method returns true if there is currently a primary clip on the clipboard. |
setPrimaryClip(ClipData clip) | This method sets the current primary clip on the clipboard. |
setText(CharSequence text) | This method can be directly used to copy text into the clipboard. |
getText() | This method can be directly used to get the copied text from the clipboard. |
***For clearing clipboard data just send or set null or empty value.
In this tutorial, we are going to learn how to copy data to Clipboard and fetch copied data from Clipboard.
1. Create a new project in Android Studio by navigating to File ⇒ New ⇒ New Project and fill required details. By default my activity is MainActivity.java.
2. Open strings.xml located under res=>values folder and add following strings.
1 2 3 4 5 6 7 8 |
<resources> <string name="app_name">Clipboard Demo</string> <string name="enter_text">Enter text here</string> <string name="copy_text">Copy to Clipboard</string> <string name="show_copied_text">Show Clipboard Data</string> <string name="paste_clipboard_data">Paste your clipboard data directly here</string> </resources> |
3. Now create a layout xml file naming activity_main.xml and add this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<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" android:orientation="vertical" android:padding="10dp" tools:context=".MainActivity"> <EditText android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/enter_text" android:inputType="text" android:padding="15dp" android:textColor="@android:color/black" android:textColorHint="@android:color/darker_gray" android:textSize="15sp" /> <Button android:id="@+id/copy_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/copy_text" android:textColor="@android:color/black" android:textSize="14sp" /> <View android:layout_width="fill_parent" android:layout_height="1px" android:layout_marginBottom="7dp" android:layout_marginTop="10dp" android:background="@android:color/darker_gray" /> <Button android:id="@+id/show_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/show_copied_text" android:textColor="@android:color/black" android:textSize="14sp" /> <!-- TextView for displaying Clipboard Data --> <TextView android:id="@+id/clipboard_data" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:padding="10dp" android:textColor="@android:color/black" android:textSize="15sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="5dp" android:text="OR" android:textColor="@android:color/black" android:textSize="13sp" /> <!-- You can directly paste your clipboard data here --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/paste_clipboard_data" android:inputType="text" android:padding="15dp" android:textColor="@android:color/black" android:textColorHint="@android:color/darker_gray" android:textSize="15sp" /> </LinearLayout> |
4. Now come to your MainActivity.java and the following code. In this class we are calling Clipboard_Utils.java for handling all Clipboard copy/paste process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.clipboard_demo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements OnClickListener { private static EditText text; private static Button copy_text, show_text; private static TextView clipboardData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); setClickListeners(); } /* Init all views */ private void initViews() { text = (EditText) findViewById(R.id.text); copy_text = (Button) findViewById(R.id.copy_text); show_text = (Button) findViewById(R.id.show_text); clipboardData = (TextView) findViewById(R.id.clipboard_data); } /* Set Click Listeners */ private void setClickListeners() { copy_text.setOnClickListener(this); show_text.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.copy_text: //Copy data to Clipboard String data = text.getText().toString().trim(); if (!data.equals("")) Clipboard_Utils.copyToClipboard(MainActivity.this, data); break; case R.id.show_text: //Get Data from Clipboard and set over TextView String text = Clipboard_Utils.getDataFromClipboard(MainActivity.this); if (!text.equals("")) clipboardData.setText(text); else Toast.makeText(MainActivity.this, "Clipboard is empty.", Toast.LENGTH_SHORT).show(); break; } } } |
5. Finally create a java class naming Clipboard_Utils.java for handling all Clipboard stuff and add the following code. In this class we are using methods for copying data to clipboard and fetching data from clipboard.
Since there are two ways of doing this so we need to check SDK versions and call the method according to API Level.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
package com.clipboard_demo; import android.annotation.TargetApi; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.os.Build; import android.widget.Toast; /** * Created by SONU on 22/03/16. */ public class Clipboard_Utils { /** * Copy Data to Clipboard Methods **/ //First Check SDK version because SDK below honeycomb has different method and above honeycomb is different public static void copyToClipboard(Context mContext, String data) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { copyToClipboardHoneyLess(mContext, data); } else { copyToClipboardForHoney(mContext, data); } Toast.makeText(mContext, "Data copied to Clipboard.", Toast.LENGTH_SHORT).show(); } //For Honeycomb and above devices @TargetApi(Build.VERSION_CODES.HONEYCOMB) private static void copyToClipboardForHoney(Context mContext, String data) { ClipboardManager clipboard = (ClipboardManager) mContext .getSystemService(Context.CLIPBOARD_SERVICE);//Get Clipboard Manager ClipData clip = ClipData.newPlainText( "clipboard data ", data);//Save plain text data to clip data clipboard.setPrimaryClip(clip);//set clip data as primary clip } //For below Honeycomb devices @SuppressWarnings("deprecation") private static void copyToClipboardHoneyLess(Context mContext, String data) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) mContext .getSystemService(Context.CLIPBOARD_SERVICE);//get Clipboard manager clipboard.setText(data);//set text to clipboard } /** * Get Data from Clipboard Methods **/ //First Check SDK version because SDK below honeycomb has different method and above honeycomb is different public static String getDataFromClipboard(Context mContext) { String text = ""; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { text = getClipboardDataHoneyLess(mContext); } else { text = getClipboardDataForHoney(mContext); } Toast.makeText(mContext, "Data pasted from Clipboard.", Toast.LENGTH_SHORT).show(); return text; } @TargetApi(Build.VERSION_CODES.HONEYCOMB) private static String getClipboardDataForHoney(Context mContext) { ClipboardManager clipboard = (ClipboardManager) mContext .getSystemService(Context.CLIPBOARD_SERVICE);//get Clipboard manager ClipData abc = clipboard.getPrimaryClip();//Get Primary clip ClipData.Item item = abc.getItemAt(0);//Get item from clip data return item.getText().toString(); } @SuppressWarnings("deprecation") private static String getClipboardDataHoneyLess(Context mContext) { android.text.ClipboardManager clipboard = (android.text.ClipboardManager) mContext .getSystemService(Context.CLIPBOARD_SERVICE);//get Clipboard manager return clipboard.getText().toString(); } } |
6. Now you are all done. Run the above code and you will get the same output as shown in video.
Thanks.
Subscribe to us and get the latest news.
2 Comments
Beta
Tuesday, May 23rd, 2017Great job!!! Very good tutorial man! I am waiting on new a new tutorial. 🙂
Neville Micallef
Monday, July 10th, 2017What is the use to declare static members?