1

I have the problem with TextView inside ListView below.

In listview, I have a TextView and I have set

android:textIsSelectable="true"

But, when I long click on TextView, I see log print

TextView: TextView does not support text selection. Action mode cancelled.

So I can not use Copy and Paste function for TextView.

If you have any idea to resolve this issue, please give some hint to fix it.

Thanks so much

Nandkishor mewara
  • 2,552
  • 16
  • 29

2 Answers2

0

Just try this...

  ListView list = (ListView) findViewById(R.id.yourList);    
    list.setOnItemLongClickListener(new OnItemLongClickListener() {
    public void onItemLongClick(AdapterView<?> a, View v, int position,long id) {
                    TextView yourFirstTextView = (TextView) v.findViewById(R.id.yourFirstTextViewId);                    
                    copyTextToClipboard(yourFirstTextView);                   

    public void copyTextToClipboard(TextView txtView){
         int sdk = android.os.Build.VERSION.SDK_INT;
         if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
             android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
             clipboard.setText(txtView.getText().toString());
         } else {
             android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
             android.content.ClipData clip = android.content.ClipData.newPlainText("text label",txtView.getText().toString());
             clipboard.setPrimaryClip(clip);
         }
    }

May it work.

  • Thanks @SJ143 : I don't want to copy all text, I only the part of text by select text – Khoa Nguyễn Mar 21 '16 at 05:37
  • 1
    In list.setOnItemLongClickListener, try to add textView.setTextIsSelectable(true); and remove it from xml and instead use: android:inputType="textMultiLine" –  Mar 21 '16 at 05:59
  • Hi @SJ143 : I still don't resolve this issue yet. I'm being continue search google :D – Khoa Nguyễn Mar 22 '16 at 01:06
0

TextView sets the width to "match_parent" android:layout_width="match_parent"