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

AndroidTextviewCopy Paste

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

Try android:textIsSelectable.

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

Solution 2 - Android

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.

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

Solution 4 - Android

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

Solution 5 - Android

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

Solution 6 - Android

  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

Solution 7 - Android

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.

Solution 8 - Android

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");
}

Solution 9 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUserView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidRamiReddyView Answer on Stackoverflow
Solution 3 - AndroidUserView Answer on Stackoverflow
Solution 4 - AndroidAgilanbuView Answer on Stackoverflow
Solution 5 - AndroidAZ_View Answer on Stackoverflow
Solution 6 - AndroidMichael MaoView Answer on Stackoverflow
Solution 7 - Androidnarcis dprView Answer on Stackoverflow
Solution 8 - AndroidBeeing JkView Answer on Stackoverflow
Solution 9 - AndroidrajeeshView Answer on Stackoverflow