1

I am using android:textIsSelectable="true" so that my text is eligible for copying text,On the same textview i am using onclicklistner,But that clicklistner wont work. As soon as i remove android:textIsSelectable="true" it will work fine. What i have tried set onLongpress listner to that textview and enable this property run time. Can any one tell me any other solution?

sog
  • 161
  • 4
  • 14
  • in xml- in java- textview.setOnClickListner(){} --> This wont work with property android:isTextSelectable="true" – sog Feb 25 '15 at 05:45
  • even when i set lonclicklistner, that textview will not select text for first time. if i press long on second time it will select the text. i.e TextView.SetLonClickListner(){ // Here Enable textview for selection TextView.setTextIsSelectable(true); } – sog Feb 25 '15 at 05:51
  • You will never have functionality to select text as well as setOnLongClickListener. Because when you long press how android would come to know that you want to select text or perform clickevent? So it's only possible by the way I have said in answer. – Apurva Feb 25 '15 at 05:53

2 Answers2

2

I have seen other people too having same issue. You should do something like this.

Wrap your textView with LinearLayout and then set onClickListener on LinearLayout instead of setting on textView. It will have same impact.

<LinearLayout
    android:id="@+id/layout"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <Button
        android:id="@+id/button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:textIsSelectable="true">
</LinearLayout>

New in your .class file

LinearLayout mLayout = (LinearLayout) findViewById(R.id.layout);
mLayout.setOnClickListener(new View....) {
    ...
    // Do something here.
}

This will make your textView selectable as well as clickable.

Rafal Enden
  • 3,028
  • 1
  • 21
  • 16
Apurva
  • 7,871
  • 7
  • 40
  • 59
0

you can use like this

 TextView tv;
 String stringYouExtracted = tv.getText().toString;
 int startIndex = tv.getSelectionStart();
 int endIndex = tv.getSelectionEnd();
 stringYouExtracted = stringYouExtracted.subString(startIndex, endIndex);

copy string to clipboard

if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", stringYouExtracted);
        clipboard.setPrimaryClip(clip);
}
Ram
  • 1,408
  • 13
  • 29
  • @Apurva- listner should be on that text view and not on linear layout. Neways working fine. Thnx bro!! – sog Feb 25 '15 at 06:14