Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I paste copied text in Android programmatically?

setPrimaryClip (http://developer.android.com/reference/android/content/ClipboardManager.html) method: ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); ClipData clip = ClipData. newPlainText("label", "Text to copy"); clipboard. setPrimaryClip(clip);

How do I use clipboard manager on Android?

One of these is an integrated clipboard manager. Like Gboard and the Samsung Keyboard, just tap the arrow icon in the top-left corner of your keyboard, and you'll see the Clipboard icon, among others. Tap it to access blocks of text you've copied recently, then you can paste them with one tap.

What is editable TextView in Android?

Advertisements. A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.


Try android:textIsSelectable.

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


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.


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

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