Android Clipboard Tutorial

Android Clipboard Tutorial

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 −

Copying data

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 −

The ClipData object can take these three form and following functions are used to create those forms.

ClipData FormMethod & Description
TextnewPlainText(label, text)

Returns a ClipData object whose single ClipData.Item object contains a text string.
URInewUri(resolver, label, URI)

Returns a ClipData object whose single ClipData.Item object contains a URI.
IntentnewIntent(label, intent)

Returns a ClipData object whose single ClipData.Item object contains an Intent.

Pasting data

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 −

Apart from the these methods , there are other methods provided by the ClipboardManager class for managing clipboard framework. These methods are listed below −

MethodDescription
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.

Example

In this tutorial, we are going to learn how to copy data to Clipboard and fetch copied data from Clipboard.

VIDEO DEMO

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.

3. Now create a layout xml file naming activity_main.xml and add this code.

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.

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.

6. Now you are all done. Run the above code and you will get the same output as shown in video.

Thanks. :)

Download Source Code – >

 

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)