Clipboard is used to copy data and paste it anywhere in android device.Only text is stored in clipboard.
Note that there are 2 different methods for clipboard.It depends on android SDK version.
Note that there are 2 different methods for clipboard.It depends on android SDK version.
- If SDK version is less than HONEYCOMB.
- SDK version is Greater or equal to HONEYCOMB SDK version.
- if SDK version is less than HONEYCOMB,copy below code.
String copyData = "hi,how are u?";
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(copyData);
Toast.makeText(ScanActivity.this,"Text copied", Toast.LENGTH_SHORT).show();
2. SDK version is Greater or equal to HONEYCOMB SDK version.
String copyData = "hi,how are u?";
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("label",copyData);
clipboard.setPrimaryClip(clip);
Toast.makeText(ScanActivity.this,"Text copied", Toast.LENGTH_SHORT).show();
Full Source Code for both version method is given below,Just copy it on your code.
String finalData = "hi,how are u?";
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(finalData);
Toast.makeText(ScanActivity.this,"Text copied", Toast.LENGTH_SHORT).show();
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("label",finalData);
clipboard.setPrimaryClip(clip);
Toast.makeText(ScanActivity.this,"Text copied", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "Copy",Toast.LENGTH_SHORT).show();
- Done ! now go to message activity, press and hold in EditText. Now past clipboard text into it.
clipboard.setText(copyData);
Toast.makeText(ScanActivity.this,"Text copied", Toast.LENGTH_SHORT).show();
good and very helpful!
ReplyDelete