TextView selection with Spannable and LinkMovementMethod TextView selection with Spannable and LinkMovementMethod android android

TextView selection with Spannable and LinkMovementMethod


Did you try setting the background of your TextView to Android's default list_selector_background?

    textView.setMovementMethod(LinkMovementMethod.getInstance());    textView.setBackgroundResource(android.R.drawable.list_selector_background);

For me it seems to give the result that you want.

UPDATE: After seeing that the item is not just a single TextView

Well, this is not a perfect solution (since it's probably better to fix the TextView - ListView highlighting interaction somehow), but it works well enough.

I figured out that instead of setting the movement method on the TextView (that triggers the issue), it is just simpler to check in the ListView's onItemClick() (after a click is definitely confirmed) to see if we should launch the onClick() on our ClickableSpans:

public class MyActivity extends Activity {    private final Rect mLastTouch = new Rect();    private boolean spanClicked(ListView list, View view, int textViewId) {        final TextView widget = (TextView) view.findViewById(textViewId);        list.offsetRectIntoDescendantCoords(widget, mLastTouch);        int x = mLastTouch.right;        int y = mLastTouch.bottom;        x -= widget.getTotalPaddingLeft();        y -= widget.getTotalPaddingTop();        x += widget.getScrollX();        y += widget.getScrollY();        final Layout layout = widget.getLayout();        final int line = layout.getLineForVertical(y);        final int off = layout.getOffsetForHorizontal(line, x);        final Editable buffer = widget.getEditableText();        final ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);        if (link.length == 0) return false;        link[0].onClick(widget);        return true;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // ...        listView.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                if (spanClicked(listView, view, R.id.details)) return;                // no span is clicked, normal onItemClick handling code here ..                                }        });        listView.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                if (event.getAction() == MotionEvent.ACTION_UP) {                    mLastTouch.right = (int) event.getX();                    mLastTouch.bottom = (int) event.getY();                }                return false;            }        });        // ...    }}

The spanClicked() method is basically an abbreviated version of the LinkMovementMethod's onTouchEvent() method in the framework. To capture the last MotionEvent coordinates (to check for the click event), we simply add an OnTouchListener on our ListView.


Use TextView's setHighlightColor(Color.TRANSPARENT);


I think best way to solve this problem is using a selector. you can chan selection backgroun color so they will seen same

example selector code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"       android:color="#000000" /> <!-- pressed -->    <item android:state_focused="true"       android:color="#000000" /> <!-- focused -->    <item android:color="#FFFFFF" /> <!-- default --></selector>

implement sth like that to your link.

let me know about result