How do I enable standard copy paste for a TextView in Android?
I want to enable standard copy paste for a TextView (the same as for EditText). How can I do it?
I tried using a non-editable EditText but it didn't work well (sometimes it became editable or the copy paste overlay was not shown). And it's probably not a good approach generally.
Need a working solution starting at API 7.
Try android:textIsSelectable
.
i.e., android:textIsSelectable="true"
To enable the standard copy/paste for TextView, U can choose one of the following:
-
Change in layout file: add below property to your TextView
android:textIsSelectable="true"
In your Java class write this line to set it programmatically.
myTextView.setTextIsSelectable(true);
And long press on the TextView you can see copy/paste action bar.
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();
}
});