How to enable standard copy for TextView in Android?

I want to include a standard copy for TextView (same as for EditText). How can i do this?

I tried using the editable EditText, but it did not work (sometimes it was edited or the overlay on the copy was not shown). And this is probably not a very good approach.

A working solution is required starting with API 7.

+50
android copy-paste textview
Apr 30 2018-12-12T00:
source share
7 answers

This works for copying 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(); } }); 
+15
Aug 21 2018-12-12T00:
source share
+106
Apr 30 '12 at 15:45
source share

To enable standard copy / paste for TextView, U can choose one of the following values:

  • Change in layout file: add property below to your TextView

    android:textIsSelectable="true"

  • In your Java class, write this line to install it programmatically. myTextView.setTextIsSelectable(true);

And a long press on the TextView you can see the copy / paste action bar.

+42
Mar 05 '14 at 7:29
source share

Requires API 11 , Updated code, previous method deprecated

Solution for full screen themes without ActionBar

Extend TextView and in the designer passport execute the following code

 this.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { ClipboardManager cManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); ClipData cData = ClipData.newPlainText("text", getText()); cManager.setPrimaryClip(cData); Util.toast(mContext, string.text_copyed); return true; } }); 
+6
Nov 14 '14 at 2:47
source share
  • use theme

     @android:style/Theme.Black.NoTitleBar.Fullscreen 

    or

     @android:style/Theme.WithActionBar 
  • set TextView to xml

     android:textIsSelectable="true" 
  • see result

+2
Nov 23 '15 at 8:33
source share

For edit text, in the manifest inside the android activity use: windowSoftInputMode = "adjustResize"

0
Mar 06 '15 at 12:22
source share

this is better:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); final android.content.ClipData clipData = android.content.ClipData .newPlainText("text label", "text to clip"); clipboardManager.setPrimaryClip(clipData); } else { final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context .getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager.setText("text to clip"); } 
0
Nov 30 '15 at 3:35
source share



All Articles