16

I have a ListView where each item is a TextView.

I want to enable the long press behaviour similar to an EditText that displays the default context menu with items like "Select all", "Cut all", "Copy all", etc.

Is there an easy way to enable this for a TextView?

Ibungo
  • 1,137
  • 12
  • 23
Erdal
  • 1,472
  • 3
  • 16
  • 33
  • at the moment I am registering for a context menu and using the clipboard manager to copy the entire text of the TextView. But it seems that there should be a default way of doing this. – Erdal Jun 01 '10 at 21:16
  • Actually, the context menu is adapted for that. You can customize it at your leisure. You might also have a look at the AlertDialog class. It's not usually used for this purpose, but you could call it with a long click. However, if I remember well, you are limited 3 buttons max. – Sephy Jun 01 '10 at 22:04
  • One more thing, I don't know if it's advisable to do that, but you could try to put your listview in a framelayout and add another view/layout inside but with a gone visibility, and turn it's visibility to visible when an element of your list is clicked and update its content? – Sephy Jun 01 '10 at 22:13
  • how did you solve this issue? – pandre May 30 '11 at 16:52

6 Answers6

21

I think I have a solution. Just call
registerForContextMenu(yourTextView);

and your TextView will be registered for receiving context menu events.

Then override onCreateContextMenu in your Activity:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    //user has long pressed your TextView
    menu.add(0, v.getId(), 0, "text that you want to show in the context menu - I use simply Copy");

    //cast the received View to TextView so that you can get its text
    TextView yourTextView = (TextView) v;

    //place your TextView's text in clipboard
    ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
    clipboard.setText(yourTextView.getText());
}

Hope this helps you and anyone else looking for a way to copy text from a TextView

pandre
  • 6,685
  • 7
  • 42
  • 50
  • Thanks for the post, just what I needed for API level 2.1. At first it didn't work, found out I had to remove this from the manifest xml: android:configChanges="keyboardHidden" – Alan Feb 29 '12 at 22:20
  • 4
    It does work. Although it copies the text before the user actually clicks copy. That could be confusing and overwrite their clipboard. Another way is to perform the copy in onContextItemSelected. You're already setting the menu option id to the view id, but not using it. Also, make sure you import android.text.ClipboardManager instead of android.content.ClipboardManager for this example if you're targeting devices < API 11. – Mike Venzke Jul 09 '12 at 05:39
  • How can I copy only when the menu item is clicked? – Raj Suvariya Dec 29 '16 at 12:55
8

To allow users to copy some or all of the TextView's value and paste it somewhere else,

set the XML attribute {@link android.R.styleable#TextView_textIsSelectable android:textIsSelectable} to "true"

or

call {@link #setTextIsSelectable setTextIsSelectable(true)}.

MichaelS
  • 5,941
  • 6
  • 31
  • 46
Samuel
  • 81
  • 1
  • 2
7

Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My code is here:

R.layout.edittext.xml

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:editable="false" 
android:background="@null"
android:textColor="#FFFFFF"/>

ListItemCopyTextActivity.java

public class ListItemCopyTextActivity extends Activity {    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout ll = new LinearLayout(this);
    ListView lv = new ListView(this);

    String[] values = new String[15];
    for (int i = 0; i < 15; i++) {
        values[i] = "ListItem NO." + i;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.edittext, values);
    lv.setAdapter(adapter);

    ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

    setContentView(ll);

    }
}

You can just long click the item, and choose the select text, copy, cut, past etc.

Ruobin Wang
  • 358
  • 3
  • 9
  • 2
    Doesn't work for me. If editable is false I get "TextView does not support text selection. Action mode cancelled." Using android:textIsSelectable is a good solution for me. – Edwin Evans Nov 04 '14 at 01:08
1

You might want to register an onItemLongClickListener on your ListView and then based on the selected item, provide the user with whatever options you choose.

Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
0

I have a solution, but I'm not exactly to useful.

just use this method :

txtDescDetail.setCursorVisible(true);

i hope to do it.

0

Here is the solution

 <TextView
        android:id="@+id/textID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:clickable="true"
        android:focusable="true"
        android:text="Terms and Conditions"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

override setOnLongClickListener

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    textID.setTextIsSelectable(true)
    textID. setOnLongClickListener {
       val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
       val clip = ClipData.newPlainText("Copied String",  textID.text)
       clipboardManager.setPrimaryClip(clip)
       true // Or false if not consumed
   }
}

the expected behavior will be like the image below

enter image description here

Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
  • Just add: android:textIsSelectable="true" to your TextView layout. It's quiet enough! No need else layout changing or code lines. – Vitaly Jan 29 '21 at 08:14