Select + copy text to TextView?

Is there a way to allow the user to select / copy text in a TextView? I need the same EditText functionality where you can press the control button for a long time and get the select all / copy popup options, but I need the control to look like a TextView.

I tried a few things, for example, to make an EditText using the editable = "none" or inputType = "none" parameter, but they still retain the EditText background frame, which I don’t want,

thank

------- Update ----------------------

This is 99%, all I want is that the hilight selection should be visible (orange material). Also, it’s good, could live with it, though:

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" style="?android:attr/textViewStyle" android:textColor="@color/white" android:textAppearance="@android:style/TextAppearance.Medium" android:cursorVisible="false" android:background="@null" /> 

I assume that this is due to cursorVisible = "false", but without this, the cursor is present even without a selection.

+61
android
May 17 '11 at 3:06
source share
6 answers

android: textIsSelectable works (at least in ICS - I haven't tested earlier versions yet)

 <TextView android:id="@+id/deviceIdTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" android:text="" /> 
+153
Jan 23 2018-12-12T00:
source share

The text view must be enabled, customizable, longClickable and textIsSelectable

  <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:id="@+id/pwTextView" android:enabled="true" android:textIsSelectable="true" android:focusable="true" android:longClickable="true" /> 
+20
May 09 '16 at 13:11
source share

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

and your TextView will be registered to receive context menu events.

Then override onCreateContextMenu in 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 text in clipboard ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(yourTextView.getText()); } 

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

+19
May 30 '11 at 17:55
source share

I am trying to implement the same thing, and your question helped me set up the editext layout correctly. So thanks! :)

Then I realized that the highlight would actually be visible if the cursor is on. But I just want you to not want to see the cursor before clicking on the text, so I hide the cursor in the layout.xml file just like you, and added an eventlistener for a long click and displayed the cursor only at the beginning of the selection.

So, add a listener to your activity in the onCreate section:

 public TextView htmltextview; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ... htmltextview.setOnLongClickListener(new OnLongClickListener(){ public boolean onLongClick(View v) { htmltextview.setCursorVisible(true); return false; } }); } 

And at the beginning there is no cursor at the beginning, and if you press for a long time, the cursor appears with selection borders.

Hope I can help.

Cheers, fm

+3
May 31 '12 at 12:29
source share

I also tried to do something similar, but still needed an individual approach with manipulating text highlighting in a TextView. I triggered selection and copying in a LongClick action.

Here's how I managed to use SpannableString :

 SpannableString highlightString = new SpannableString(textView.getText()); highlightString.setSpan(new BackgroundColorSpan(ContextCompat.getColor(getActivity(), R.color.gray)) , 0, textView.getText().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); textView.setText(highlightString); copyToClipboard(urlToShare); 

and copy function:

 public void copyToClipboard(String copyText) { ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("url", copyText); clipboard.setPrimaryClip(clip); Toast toast = Toast.makeText(getActivity(), "Link is copied", Toast.LENGTH_SHORT); toast.show(); } 

I hope this helps someone who finishes this question :)

+1
Jan 24 '17 at 17:07 on
source share

Just use this simple library: GitHub: Selectable TextView

-2
Jan 21 '16 at 3:41
source share



All Articles