Thursday, September 2, 2010

Copy and Paste using ClipboardManager

ClipboardManager is a interface to the clipboard service, for placing and retrieving text in the global clipboard.

You do not instantiate this class directly; instead, retrieve it through getSystemService(String).

Copy and Paste using ClipboardManager

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="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<EditText
android:id="@+id/src"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/dest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/copynpaste"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Copy'n Paste"
/>
</LinearLayout>


AndroidCopynPaste.java
package com.exercise.AndroidCopynPaste;

import android.app.Activity;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidCopynPaste extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final EditText src = (EditText)findViewById(R.id.src);
final TextView dest = (TextView)findViewById(R.id.dest);
Button copynPaste = (Button)findViewById(R.id.copynpaste);

final ClipboardManager clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

copynPaste.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clipBoard.setText(src.getText());
dest.setText(clipBoard.getText());

}});
}
}



Download the files.

2 comments:

luca said...

hi could you check the download link please ? thank you !!!

vicky007 said...

I want to copy smiley stored in my drawable resource folder. Please provide me an example of how to do that.