Android Clipboard

Android Clipboard

Android Clipboard are provided by Android for copying and pasting different kinds of text data. The info could be text message, images, binary stream data or other intricate data types.Google android supplies the collection of ClipData and android Clipboard Manager and ClipData.item to work with the copying and pasting framework.To be able to use android clipboard platform, you need to place data into clip thing, and put that thing into system extensive clipboard then.

To be able to use android clipboard , you will need to instantiate an thing of android Clipboard Manager by getting in touch with the getSystemService() method. Its syntax is listed below

ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

The next matter you must do is to instantiate the ClipData subject by dialling the respective kind of data approach to the ClipData class. In case there is words data , the newPlainText method will be called. You then have to create that data as the clip of the android Clipbaoard Director thing.Its syntax is listed below

ClipData myClip;
String text = "hello world";
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

 

ClipData Object may take these three form and next functions are being used to produce those forms.

ClipData Form & Method

Text

newPlainText

Results a ClipData thing whose solitary ClipData.Item subject contains a words string.

URI

newUri

Comes back a ClipData subject whose solitary ClipData.Item subject has a URI.

Intent

newIntent

Results a ClipData thing whose solitary ClipData.Item subject contains an Objective.

Anroid Clipboard – Pasting data

To be able to paste the info, we will first obtain the clip by dialling the getPrimaryClip() method. And from that click we are certain to get that in ClipData.Item object. And from the thing we will get the info. Its syntax is listed below

ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();

 

From the these procedures aside , there are other methods provided by the android Clipboard Manager category for controlling clipboard framework. These procedures are the following

Methods & Descriptions

getPrimaryClip()

This technique returns the existing key clip on the android clipboard just

getPrimaryClipDescription()

This method comes back a information of the existing key clip on the android clipboard however, not a duplicate of its data.

hasPrimaryClip()

This technique results true when there is presently an initial clip on the android clipboard

setPrimaryClip()

This method models the current principal clip on the android clipboard

setText()

This technique can be straight used to duplicate text message in to the android clipboard

getText()

This technique can be straight used to get the copied words from the clipboard

Android Clipboard Example

Here is a good android clipboard example demonstrating the utilization of ClipboardManager. It creates a simple copy paste program that allows anyone to copy the written text and then paste it via clipboard.

To test out this android clipboard example , you can run this on a genuine device or within an emulator.

Steps     Descriptions

  1. You use Android studio room IDE to set-up an Android software and under a package deal com.example.sappal.myapplication.
  2. Modify src/MainActivity.java to include necessary code.
  3. Modify the res/structure/activity_main to include particular XML components
  4. Run the application form and choose a jogging google android device and set up the application onto it and validate the results

Following is this content of the customized main activity src/MainActivity.java.

package com.example.sappal.myapplication;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;

import android.os.BatteryManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Set;


public class MainActivity extends ActionBarActivity {
   EditText ed1,ed2;
   Button b1,b2;
   
   private ClipboardManager myClipboard;
   private ClipData myClip;.
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      ed1=(EditText)findViewById(R.id.editText);
      ed2=(EditText)findViewById(R.id.editText2);
      
      b1=(Button)findViewById(R.id.button);
      b2=(Button)findViewById(R.id.button2);
      
      myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
      
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            String text;
            text = ed1.getText().toString();
            
            myClip = ClipData.newPlainText("text", text);
            myClipboard.setPrimaryClip(myClip);
            
            Toast.makeText(getApplicationContext(), "Text Copied",Toast.LENGTH_SHORT).show();
         }
      });
      
      b2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            ClipData abc = myClipboard.getPrimaryClip();
            ClipData.Item item = abc.getItemAt(0);
            
            String text = item.getText().toString();
            ed2.setText(text);
            
            Toast.makeText(getApplicationContext(), "Text Pasted",Toast.LENGTH_SHORT).show();
         }
      });
   }
   
   @Override
   protected void onDestroy() {
      super.onDestroy();
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

Following is the modified content of the xml res/layout/activity_main.xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
   
   <TextView android:text="Example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Copy text"
      android:layout_below="@+id/imageView"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView" />
      
   <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/editText2"
      android:layout_alignLeft="@+id/editText"
      android:layout_alignStart="@+id/editText"
      android:hint="paste text"
      android:layout_below="@+id/editText"
      android:layout_alignRight="@+id/editText"
      android:layout_alignEnd="@+id/editText" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Copy text"
      android:id="@+id/button"
      android:layout_below="@+id/editText2"
      android:layout_alignLeft="@+id/editText2"
      android:layout_alignStart="@+id/editText2" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Paste text"
      android:id="@+id/button2"
      android:layout_below="@+id/editText2"
      android:layout_alignRight="@+id/editText2"
      android:layout_alignEnd="@+id/editText2" />
      
</RelativeLayout>

Following is the content of the res/values/string.xml

<resources>
   <string name="app_name">My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
</resources>

Following is the content of AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.clipboard"
   android:versionCode="1"
   android:versionName="1.0" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sappal.myapplication.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
   </application>

</manifest>

Let’s make an effort to run our android clipboard applicatoin we just developed. I assume you’d created your AVD while doing environment set up. To perform the software from Android studio room, open up one of your project’s activity data files and click Run Eclipse Runicon. Android studio room installer shall screen pursuing images

android clipboard

Now just enter in any text message in the written text to replicate field and then choose the copy content material button. The next notification will be exhibited which is shown below

android clipboard

 

Just press the paste button now, and you’ll start to see the words which is copied is pasted in neuro-scientific Copied Wording now. It really is shown below

android clipboard