androidtextviewcopy-paste

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.


Solution

  • 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();
        }
    });