- How to Use the Clipboard on Android Phones
- Using the Android clipboard is easier than you think
- What to Know
- How to Use the Clipper Clipboard Manager
- How to Clear Your Android Clipboard
- Where Is the Clipboard on an Android Phone?
- Android Clipboard with Examples
- Android Copying Data to Clipboard
- Android Pasting Data from Clipboard
- Android Clipboard App Example
- activity_main.xml
- MainActivity.java
- Output of Android Clipboard App Example
How to Use the Clipboard on Android Phones
Using the Android clipboard is easier than you think
What to Know
- Easiest: Download Clipper Clipboard Manager or an alternative app from Google Play.
- Highlight the text, long-press the selected text, then choose Copy. Long-press an empty field and select Paste to insert the copied text.
- Alternative method: Use the Gboard keyboard to manage the clipboard.
This article explains how to use Clipper Clipboard Manager to access the clipboard history on an Android and clear it. Additional information covers how to use the Android Gboard keyboard if you don’t use a clipboard manager app.
How to Use the Clipper Clipboard Manager
The easiest way to access your clipboard history is to use a clipboard manager app. Although Clipper Clipboard Manager is one of the easiest to use, Google Play has many alternatives to choose from.
Log in to Google Play and install the Clipper Clipboard Manager app.
Launch the Clipper Clipboard Manager. When you long-press and copy text to save to the clipboard, it appears in the Clipboard log inside the app.
Press the three dots to the right of the clipboard snippet to open a menu with more options.
In this menu, you can View, Edit, Share, or Select that entry so that you can paste it anywhere you like.
Without a clipboard manager like Clipper, you’d only have access to the last item that you copied to the clipboard. However, a clipboard manager gives you access to the clipboard history so you can use anything you’ve recently saved there.
How to Clear Your Android Clipboard
If you’re using the Clipper Clipboard Manager, you’ll notice a Delete option when you select the three dots to the right of a selection. Use this to clear out those clipboard items.
Another solution is to enable and use the Gboard keyboard that comes with newer Android phones. If it isn’t available on yours, you can install Gboard on Google Play.
Open the messaging app on your Android, and press the + symbol to the left of the text field.
Select the keyboard icon. When the keyboard appears, select the > symbol at the top. Here, you can tap the clipboard icon to open the Android clipboard.
If you’ve never used the clipboard before on your phone, you’ll see a notification to turn on the Gboard clipboard. To do so, tap Turn on clipboard.
With the clipboard on, any time you copy something to the clipboard and then tap the clipboard on the Google Android keyboard again, you’ll see a history of all recent items you’ve added.
To delete any of these items from the clipboard, first tap the edit icon.
Select each item you want to delete and tap the trash icon to delete them.
The clipboard manager that comes with your built-in keyboard app depends largely on your Android phone version and brand. For example, the Samsung Keyboard features a clipboard manager tool as well. The keyboard is typically the primary way to access and manage your clipboard without an app.
Where Is the Clipboard on an Android Phone?
When you save text to the clipboard on your Android, the clipboard service stores the information in RAM. On stock Android phones, you can’t access that data directly. On Samsung phones, the clipboard history exists in a file in the /data/Clipboard directory.
Even on a Samsung phone, that file is not accessible without rooting the phone, but you can access your clipboard history on any Android phone by using a clipboard manager app.
If you’re using an Android that stores the clipboard in a system folder, you may be able to view the file using Minimal ADB and Fastboot. However, since the clipboard folder is a root folder and accessible only with root access, it’s likely that even though you can see the file with ADB, you won’t be able to open it.
Источник
Android Clipboard with Examples
In android, Clipboard is a framework that is useful for copying and pasting the different types of data such as text strings, images, binary stream data, and other complex data types.
Generally, the android Clipboard framework will store the simple text data directly in the clipboard and the complex data is stored as a reference that the pasting application resolves with a content provider.
In android, the clipboard copying and pasting works within an application and between the applications that implement the framework.
To use the android clipboard framework, we need to put the data into a clip object, and then put the clip object on the system-wide clipboard.
The clip object in android can take one of three forms to copy and paste data in android applications.
Clip Data Form | Description |
---|---|
Text | It’s a text string. We can put the string directly into the clip object, and then put the clip object on the clipboard. To paste the string, we can get the clip object from the clipboard and copy the string to our application’s storage. |
URI | It represents any form of URI and it’s primarily for copying the complex data from a content provider. To copy the data, we need to put a Uri object into a clip object, and then put the clip object onto the clipboard. To paste the data, we need to get the clip and Uri objects, resolve it to a data source such as a content provider, and copy the data from the source into our application’s storage. |
Intent | To copy data, we need to create an Intent object, put it into a clip object, and then put the clip object onto the clipboard. To paste the data, we need to get the clip object and then copy the Intent object into our application’s memory area. |
In android, the clipboard can hold only one clip object at a time. When an application puts a clip object on the clipboard, the previous clip object disappears.
To use the clipboard in our applications, we need to create an instance of ClipboardManger class by calling the getSystemService() method.
Following is the syntax of creating an instance of ClipboardManager by calling the getSystemService() method.
If you observe above code snippet, we created an instance of ClipboardManager by calling getSystemService() method.
Android Copying Data to Clipboard
As discussed, to copy the data to clipboard we need to create an instance of ClipData object by calling the respective type of data method of ClipData class. In case if it is text type, then newPlainText method will be called, and add the finished ClipData object to the ClipboardManager object using the setPrimaryClip method.
Following is the code snippet of copying the text data to clipboard in android applications.
ClipboardManager clipboardManager ;
ClipData clipData ;
String txtcopy = «Welcome to tutlane» ;
clipData = ClipData.newPlainText( «text» ,txtcopy);
clipboardManager .setPrimaryClip( clipData );
If you observe above code snippet, we copied text type of data to ClipData object and added ClipData object to the ClipboardManager object using setPrimaryClip method to copy the data to clipboard.
Android Pasting Data from Clipboard
As described previously, you paste data from the clipboard by getting the global clipboard object, getting the clip object, looking at its data, and if possible copying the data from the clip object to your own storage.
As discussed, to paste the data from clipboard we need to create an instance of ClipData object to get the clip data from ClipboardManager object using the getPrimaryClip() method. If data available, get the data from clip data using ClipData.Item object. After that copy its text to your own storage using getText().
Following is the code snippet to pasting the data from the clipboard in android applications.
ClipData pData = clipboardManager .getPrimaryClip();
ClipData.Item item = pData.getItemAt( 0 );
String txtpaste = item.getText().toString();
If you observe above code snippet, we are getting the clip data from ClipboardManager object using getPrimaryClip() method. After that, we are getting the data from ClipData object using ClipData.Item and getText() method.
Now we will see how to use the clipboard for copying and pasting text type data in the android application with examples.
Android Clipboard App Example
Following is the example of copying the text type data into the clipboard from edittext on button click and pasting the clipboard data into another edittext control on button click in android application.
Create a new android application using android studio and give names as ClipboardExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Once we create an application, open activity_main.xml file from \res\layout folder path and write the code like as shown below.
activity_main.xml
xml version= «1.0» encoding= «utf-8» ?>
LinearLayout xmlns: android = «http://schemas.android.com/apk/res/android»
android :orientation= «vertical» android :layout_width= «match_parent»
android :layout_height= «match_parent» >
TextView
android :id= «@+id/fstTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :layout_marginTop= «150dp»
android :text= «Enter Text to Copy»
/>
EditText
android :id= «@+id/txtCopy»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :text= «Welcome to Tutlane»
android :ems= «10» >
EditText >
Button
android :id= «@+id/btnCopy»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :text= «Copy Data to Clipboard»/>
TextView
android :id= «@+id/secTxt»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :text= «Show Copied Data»
android :layout_marginLeft= «100dp»
/>
EditText
android :id= «@+id/txtShow»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :ems= «10»/>
Button
android :id= «@+id/btnShow»
android :layout_width= «wrap_content»
android :layout_height= «wrap_content»
android :layout_marginLeft= «100dp»
android :text= «Show Clipboard Data»/>
LinearLayout >
Now open your main activity file MainActivity.java from \java\com.tutlane.clipboardexample path and write the code like as shown below
MainActivity.java
package com.tutlane.clipboardexample;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity <
private EditText ctxt ;
private EditText ptxt ;
private Button btncpy ;
private Button btnpst ;
private ClipboardManager clipboardManager ;
private ClipData clipData ;
@Override
protected void onCreate(Bundle savedInstanceState) <
super .onCreate(savedInstanceState);
setContentView(R.layout. activity_main );
ctxt =(EditText)findViewById(R.id. txtCopy );
ptxt = (EditText)findViewById(R.id. txtShow );
btncpy = (Button)findViewById(R.id. btnCopy );
btnpst = (Button)findViewById(R.id. btnShow );
clipboardManager = (ClipboardManager)getSystemService(Context. CLIPBOARD_SERVICE );
btncpy .setOnClickListener( new View.OnClickListener() <
@Override
public void onClick(View v) <
String txtcopy = ctxt .getText().toString();
clipData = ClipData.newPlainText( «text» ,txtcopy);
clipboardManager .setPrimaryClip( clipData );
Toast.makeText(getApplicationContext(), «Data Copied to Clipboard» , Toast. LENGTH_SHORT ).show();
>
>);
btnpst .setOnClickListener( new View.OnClickListener() <
@Override
public void onClick(View v) <
ClipData pData = clipboardManager .getPrimaryClip();
ClipData.Item item = pData.getItemAt( 0 );
String txtpaste = item.getText().toString();
ptxt .setText(txtpaste);
Toast.makeText(getApplicationContext(), «Data Pasted from Clipboard» ,Toast. LENGTH_SHORT ).show();
>
>);
>
>
If you observe above code snippet, we used ClipboardManager, ClipData, ClipData.Item objects to copy a data into clipboard and paste a data from clipboard based on our requirements.
Output of Android Clipboard App Example
When we run above program in android studio we will get the result like as shown below.
If we enter text in the first edittext and click on Copy Data to Clipboard button, the text will be copied to the clipboard and when we click on Show Clipboard Data, the clipboard data will be pasted into second edittext control.
This is how we can use clipboard framework in android applications to copy and paste the data based on our requirements.
Источник