How to Enable Standard Copy Paste for a Textview in Android

How do I enable standard copy paste for a TextView in Android?

This works for copy pre-Honeycomb:

import android.text.ClipboardManager;

textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(textView.getText());
Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
}
});

How can i enable copy and Paste option in TextView in an App | Android Studio

Add this line to textview :

 android:longClickable="true"
android:textIsSelectable="true"

In your Java class write this line to set it programmatically.

myTextView.setTextIsSelectable(true);

How to select and copy text by pressing long in textView?

Try this , its will work for you :

 private ClipboardManager myClipboard;
private ClipData myClip;

//inside oncreate
myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

mEditText.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
String text;
text = mEditText.getText().toString();

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

Toast.makeText(getApplicationContext(), "Text Copied",
Toast.LENGTH_SHORT).show();

return true;
}
});

Select and copy text in a TextView Android

Ok so, I found the problem by myself, I wasn't able to select because i have a

setMovementMethod(new ScrollingMovementMethod());

I tried to delete this one and it's work.
Thx for ur help

How to copy text programmatically in my Android app?

Use ClipboardManager#setPrimaryClip method:

import android.content.ClipboardManager;

// ...

ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", "Text to copy");
clipboard.setPrimaryClip(clip);

ClipboardManager API reference



Related Topics



Leave a reply



Submit