154

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.

User
  • 31,811
  • 40
  • 131
  • 232
  • Is [this](http://stackoverflow.com/questions/3605155/android-copy-and-paste-with-textview) what you are looking for? – Jermin Bazazian Apr 30 '12 at 15:21
  • It's similar, but not the same because I want to copy all the text. But it looks the same. The problem is that the answer there only works starting at API 11 and I need something which works from API 7. – User Apr 30 '12 at 15:23
  • `ClipboardManager` is available since API 1 (look at [here](http://developer.android.com/reference/android/text/ClipboardManager.html) ). How about long press, with a popup to copy to clipboard? – Jermin Bazazian Apr 30 '12 at 15:40
  • @Jermin Yes, it seems I have to make a custom solution. – User May 02 '12 at 07:05

11 Answers11

284

Try android:textIsSelectable.

i.e., android:textIsSelectable="true"

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Can one do it for EditTextPreference? – powder366 Mar 09 '13 at 13:17
  • 1
    @powder366: I would think that `EditTextPreference` already supports copy/paste. It certainly seems to in the Settings app. – CommonsWare Mar 09 '13 at 13:26
  • Actually it seems to be a bug. Shows up on my Samsung 4.2.2 device. Nexus7 and Nexus10 works. https://code.google.com/p/android/issues/detail?id=26008 – powder366 Mar 09 '13 at 14:17
  • Just a note on this, if your text view is initially hidden and you are showing it programatically using setVisibility, then you need to use textView.setTextIsSelectable(true) to make this work. It won't work via xml in that case. – Praveen Singh Nov 04 '17 at 19:20
66

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

  1. Change in layout file: add below property to your TextView

    android:textIsSelectable="true"

  2. 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.

Oguz Ozcan
  • 1,694
  • 19
  • 26
RamiReddy
  • 1,069
  • 9
  • 11
27

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();
    }
});
JJD
  • 50,076
  • 60
  • 203
  • 339
User
  • 31,811
  • 40
  • 131
  • 232
  • 4
    A good strategy would be to use this along with CommonsWare's answer. Since adding `android:textIsSelectable` lives in the layout, it will just get ignored on lower API levels. Then use Ixx's solution within an if statement that checks to see if the API level is below 11. Best of both worlds. The nice thing about `android:textIsSelectable` is that it gives you the platform's text selection action bar, and has a nice native feeling. – ejf Sep 11 '12 at 00:15
  • 1
    The method setText(CharSequence) from the type ClipboardManager is deprecated – S.M_Emamian Jan 10 '15 at 19:49
  • 1
    Yes, well, this answer is from more than 2 years ago. Feel free to edit or add a new one. – User Jan 10 '15 at 19:51
14

In xml textview paste this code

android:textIsSelectable="true"

Then in java file,

 final TextView txtcopypaste = findViewById(R.id.txtcopypaste); // my textview
    txtcopypaste.setOnClickListener(new View.OnClickListener() { // set onclick listener to my textview
        @Override
        public void onClick(View view) {
            ClipboardManager cm = (ClipboardManager)getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
            cm.setText(txtcopypaste.getText().toString());              
            Toast.makeText(getApplicationContext(), "Copied :)", Toast.LENGTH_SHORT).show();
        }
    });

Requirement : Need to copy and paste the text which is in the textview.

OutCome : Using textview , once i clicked the textview. Its automatically copied the text which is in the textview.

Note: While importing clipboardmanager try to prefer

Please prefer text clipboard manager

import android.text.ClipboardManager; // prefer this 

try to avoid content clipboard manager

import android.content.ClipboardManager; // Not this
Agilanbu
  • 2,747
  • 2
  • 28
  • 33
12

Requires API 11, Updated Code, previous method is deprecated

Solution for theme full screen without ActionBar

Extend TextView and in constructor paste 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;
            }
        });
AZ_
  • 21,688
  • 25
  • 143
  • 191
5
  1. use theme

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

    or

    @android:style/Theme.WithActionBar
    
  2. set TextView in xml

    android:textIsSelectable="true"
    
  3. see result

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
Michael Mao
  • 433
  • 5
  • 9
2

if someone wants to go the extra mile and do the select and copy to the clipboard with one click :

 phone.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            ClipData clip = ClipData.newPlainText("PhoneNumber", phone.getText());
                            clipboard.setPrimaryClip(clip);

                        }
                    });

phone is the TextView and phone.Text is the Text that will be copied to the clipboard.

narcis dpr
  • 939
  • 2
  • 12
  • 32
0

Here is how you can enable the Standard Copy/Paste:

TextView class has a definition:

boolean canPaste() {
    return (mText instanceof Editable
            && mEditor != null && mEditor.mKeyListener != null
            && getSelectionStart() >= 0
            && getSelectionEnd() >= 0
            && getClipboardManagerForUser().hasPrimaryClip());
}

Which can be bypassed using:

public static void enableCopyPaste(final TextView tv) {
    if(tv.getKeyListener()==null){
        tv.setKeyListener(new KeyListener(){

                @Override
                public void clearMetaKeyState(View p1, Editable p2, int p3) {
                    // unused
                }

                @Override
                public int getInputType() {
                    return tv.getInputType();
                }

                @Override
                public boolean onKeyDown(View p1, Editable p2, int p3, KeyEvent p4) {
                    // unused
                    return false;
                }

                @Override
                public boolean onKeyOther(View p1, Editable p2, KeyEvent p3) {
                    // unused
                    return false;
                }

                @Override
                public boolean onKeyUp(View p1, Editable p2, int p3, KeyEvent p4) {
                    // unused
                    return false;
                }
            });
    }

    tv.setTextIsSelectable(true);
    CharSequence mText = tv.getText();
    if(!(mText instanceof Editable)){
        tv.setText(mText,TextView.BufferType.EDITABLE);
    }
}

By calling:

enableCopyPaste(YOUR_TEXTVIEW);

Ps: It also enables Cut, Share etc

DiLDoST
  • 335
  • 3
  • 12
-1

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");
}
Community
  • 1
  • 1
Beeing Jk
  • 3,796
  • 1
  • 18
  • 29
-1
  1. you have to set FullScreen theme on your activity or fragment first of all!

@

Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        requireActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }

and in layout.xml in textview add:

    android:textIsSelectable="true"

Done!

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
-2

For an EditText, in manifest inside the activity use android:windowSoftInputMode="adjustResize"

Xenolion
  • 12,035
  • 7
  • 33
  • 48
rajeesh
  • 937
  • 10
  • 11