How to Copy Text to Clip Board in Android?
use ClipboardManager
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
make sure you have imported android.content.ClipboardManager
and NOT android.text.ClipboardManager
. Latter is deprecated.
Check this link for Further information.
How do I paste text with Android clipboard manager in an empty input in a WebView?
This is the best solution I have found, just get the text of the clipboard and paste it throug javascript when longclick is done in a focused input of a web page loaded in myWebView
myWebView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip;
clip = clipboard.getPrimaryClip();
String str = clip.getItemAt(0).getText().toString();
myWebView.evaluateJavascript("try{document.activeElement.value='"+str+"';}catch(e){}", new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {}
});
return true;
}
});
Paste from clipboard in Android
public CharSequence getText ()
Since: API Level 11
This method is deprecated.
Use getPrimaryClip() instead. This retrieves the primary clip and tries to coerce it to a string.
String textToPaste = null;
ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard.hasPrimaryClip()) {
ClipData clip = clipboard.getPrimaryClip();
// if you need text data only, use:
if (clip.getDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))
// WARNING: The item could cantain URI that points to the text data.
// In this case the getText() returns null and this code fails!
textToPaste = clip.getItemAt(0).getText().toString();
// or you may coerce the data to the text representation:
textToPaste = clip.getItemAt(0).coerceToText(this).toString();
}
if (!TextUtils.isEmpty(textToPaste))
((TextView)findViewById(R.id.etInput1)).setText(textToPaste);
You are allowed to add additional ClipData.Item
items with text via ClipData.addItem()
, but there is no way to discern them.
How to get notified when user copied something in android?
First, You need to add these permissions to AndroidManifest:
<uses-permission android:name="android.permission.GET_CLIPS" />
<uses-permission android:name="android.permission.READ_CLIPS" />
<uses-permission android:name="android.permission.WRITE_CLIPS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Then, you need to add a service like this:
public class Clipboard extends Service {
private ClipboardManager mCM;
IBinder mBinder;
int mStartMode;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mCM = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
mCM.addPrimaryClipChangedListener(new OnPrimaryClipChangedListener() {
@Override
public void onPrimaryClipChanged() {
String newClip = mCM.getText().toString();
Toast.makeText(getApplicationContext(), newClip.toString(), Toast.LENGTH_LONG).show();
Log.i("LOG", newClip.toString() + "");
}
});
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Add this service in AndroidManifest:
<service android:name=".Clipboard" />
start service at MainActivity
startService(new Intent(this, Clipboard.class));
Copy-Paste image in Android using Clipboard Manager
This code works, just find appropriate app and OS to test it.
ClipboardManager mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ContentValues values = new ContentValues(2);
values.put(MediaStore.Images.Media.MIME_TYPE, "Image/jpg");
values.put(MediaStore.Images.Media.DATA, filename.getAbsolutePath());
ContentResolver theContent = getContentResolver();
Uri imageUri = theContent.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
ClipData theClip = ClipData.newUri(getContentResolver(), "Image", imageUri);
mClipboard.setPrimaryClip(theClip);
Edit:
According to my further investigations, however, in most of Android platforms the image copy past is not possible because original android OS does not have such a feature. The code above works only for several cases where the OS is modified.
For example, in Samsung Note tablet it works. And you can past the image in Polaris office.
But Polaris is used the hidden API provided by Samsung, as that app comes with devices.
Related Topics
Change Background of Progressdialog
Custom Converter for Retrofit 2
Checking a Checkbox in Listview Makes Other Random Checkboxes Checked Too
Register to Be Default App for Custom File Type
Upgraded to Android Studio 3.4 - Aapt2Internalexception: Aapt2: Daemon Startup Failed
Can't Upload Updated APK to Google Play Due to Different Certificate
Arrayadapter.Notifydatasetchanged() Is Not Working
Permission Denied (Missing Internet Permission): But Permission Is Given
Connecting Android with Ms SQL Server 2008
How to Use Fragments in Android
How to Change Date Time Format in Android
Android Opengl Es Transparent Background
How to Play an Mp3 in the Res/Raw Folder of My Android App
How to Correctly Start Activity from Postexecute in Android
How to Completely Kill/Remove/Delete/Stop an Asynctask