5

Currently, I am create a small app for reading. There is a textview to display text. We can bookmark text and store in a listview. I want to click from listview and it highlight or select the bookmark index in the textview. I can get the bookmark_index. Is there method like textview.setSelect(Start,Length)? or Any library can do it? Thank

Please look image view to understand my idea.

enter image description here

K.Sopheak
  • 22,904
  • 4
  • 33
  • 78

4 Answers4

1

Use Spannable

Spannable WordtoSpan = new SpannableString("partial colored text");        
WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), startIndex, stopIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(WordtoSpan);
Nitesh
  • 3,868
  • 1
  • 20
  • 26
1

All the above solutions were not working for me. Then I added android:textIsSelectable="true" in XML, but it wasn't working.

What makes it working is to add textView.setTextIsSelectable(true) in your activity or fragment or adapter.

Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
0

It depends on the minimum Android version that you'd like to support.

On 3.0+ (API Level 11 and up), you have the textIsSelectable attribute on the TextView, which enables this behavior. E.g.:

<TextView
    android:id="@+id/deviceIdTV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textIsSelectable="true"
    android:text="" />

Or you can implement this feature yourself using spans like below.

TextView myTV = (TextView)findViewById(R.id.textView1);
String  textString = "StackOverFlow Rocks!!!"; 
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTV.setText(spanText);
King of Masses
  • 18,405
  • 4
  • 60
  • 77
0

Maybe this solution will help you for provisioning both a menu or textselection.

I wanted text to be either selectable (for copy and paste) or wanted other gestures to work.

  • Set textIsSelectable to false, either in the layout file or programmatically.
  • Set an onTouchListener on the textview with your Gestures.
  • Allow one of the gestures to switch to textSelection mode. See below.

How to set textSelection programmatically?

  • Set the textIsSelectable, focusable, longPressable to true
  • Set the onTouchListener to null.
  • Install a click listener for switching back to the normal gestures / menu.

1) Install your GestureHandler:

// Create your Touch Listener  
onTouchListener = new OnSwipeTouchListener(mCtx, this);
view.setOnTouchListener( onTouchListener);

2) Switch to textselction modus:

// Create your popup with an menu option to switch to textselection modus:
PopupMenu popup = new PopupMenu(mCtx, view);
popup.inflate(R.menu.text_options_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case ...
            case R.id.text_textisselectable:
                view.setOnTouchListener(null);
                ((TextView)view).setTextIsSelectable( true);
                ((TextView)view).setFocusable( true);
                ((TextView)view).setLongClickable( true);
                // Install a click listener to switch back to the previous Touch Listener
                ((TextView)view).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        PopupMenu popupx = new PopupMenu(mCtx, view);
                        popupx.inflate(R.menu.selecttext_back_menu);
                        popupx.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                ((TextView)view).setTextIsSelectable( false);
                                ((TextView)view).setFocusable( false);
                                ((TextView)view).setLongClickable( false);
                                view.setOnTouchListener(onTouchListener);
                                return true;
                            }});
                            popupx.show();
                        }
                    });
                break;
tm1701
  • 7,307
  • 17
  • 79
  • 168