Copy and Paste Text Android Example
This post about how to copy and paste text from the Edit Text. I have created two buttons copy and paste.
one edit text and textview.
Source Code
Screen Shot
This post about how to copy and paste text from the Edit Text. I have created two buttons copy and paste.
one edit text and textview.
Source Code
package com.example.sleep;
/**
* @iamvijayakumar
*/
import
android.annotation.SuppressLint;
import
android.app.Activity;
import
android.content.ClipData;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
public class CopyAndPaste extends Activity {
EditText editText;
TextView pasteText;
Button copyBtn, pasteBtn;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.copy_paste);
copyBtn = (Button)
findViewById(R.id.copy_btn);
pasteBtn = (Button)
findViewById(R.id.paste_btn);
editText = (EditText)
findViewById(R.id.editText);
pasteText = (TextView)
findViewById(R.id.pate_textview);
copyBtn.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
int sdk =
android.os.Build.VERSION.SDK_INT;
if (sdk <
android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager
clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(editText.getText());
} else {
android.content.ClipboardManager
clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
android.content.ClipData
clip = android.content.ClipData
.newPlainText("MyText", editText.getText());
clipboard.setPrimaryClip(clip);
}
}
});
pasteBtn.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
int sdk =
android.os.Build.VERSION.SDK_INT;
if (sdk <
android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager
clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
String
pasteData = clipboard.getText().toString();
pasteText.setText(pasteData);
} else {
android.content.ClipboardManager
clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData.Item
item = clipboard.getPrimaryClip()
.getItemAt(0);
String
pasteData = item.getText().toString();
pasteText.setText(pasteData);
}
}
});
}
}
Screen Shot
No comments:
Post a Comment