35

I've got little problem, i need to remove or customize this orange highlight during clicking on clickablespan. This is my class extending ClickableSpan

public class InternalClickableSpan extends ClickableSpan {

    private String clicked;

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }

    public InternalClickableSpan(String clickedString) {
        clicked = clickedString;
    }

    @Override
    public void onClick(View view) {
        Selection.setSelection((Spannable) ((TextView)view).getText(), 0);
        Toast toast = Toast.makeText(mContext, clicked, Toast.LENGTH_SHORT);
        toast.show();
    }
}

and this is how i use it on text view

Spannable spans = (Spannable) tv.getText();      
spans.setSpan(new InternalClickableSpan(contacts[i]), text.indexOf(contacts[i]),   text.indexOf(contacts[i])+contacts[i].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Does anybody know how to customize "onclick highlight" on spannable object?

edit: Thanks Aleadam for response, i'am overriding updateDrawState (please take a look at the first method in my InternalClickableSpan class), but i can't find a way to customize this higlight anyway. Has anybody got other ideas? Thanks

inazaruk
  • 74,247
  • 24
  • 188
  • 156
Robert
  • 351
  • 1
  • 3
  • 3
  • Try overriding updateDrawState()? http://developer.android.com/reference/android/text/style/ClickableSpan.html#updateDrawState%28android.text.TextPaint%29 – Aleadam Apr 08 '11 at 13:54
  • @Aleadam thanks for response, i'am overriding updateDrawState (please take a look at the first method in my InternalClickableSpan class), but i can't find a way to customize this higlight anyway. Has anybody got other ideas? Thanks – Robert Apr 11 '11 at 04:59
  • 13
    Think i got it, to disable higlight we need to set setHighlightColor(Color.TRANSPARENT) method on TextView – Robert Apr 15 '11 at 08:29

7 Answers7

31

You can override onClick(View widget) like this:

        @Override
        public void onClick(View widget) {
            // do what must happen after click event.
            widget.invalidate();
        }
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
24

This will remove any highlight.

tv.setHighlightColor(Color.TRANSPARENT);
Tuss
  • 935
  • 1
  • 8
  • 14
15

enter image description here

ClickableSpan linkClick = new ClickableSpan() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(), "Link Click",
                Toast.LENGTH_SHORT).show();
        view.invalidate();
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        if (textView.isPressed()) {
            ds.setColor(Color.BLUE);
        } else {
            ds.setColor(Color.RED);
        }
        textView.invalidate();
    }
};
textView.setHighlightColor(Color.TRANSPARENT);

Spannable spannableString = new SpannableString("Link in TextView");
spannableString.setSpan(linkClick, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString, TextView.BufferType.SPANNABLE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
aldok
  • 17,295
  • 5
  • 53
  • 64
Linh
  • 57,942
  • 23
  • 262
  • 279
0

just use this..

view.setSelector(new ColorDrawable(Color.TRANSPARENT));
Sunil Pandey
  • 436
  • 4
  • 9
0

u can replace default highlightColor android:textColorHighlight

    <TextView
    android:id="@+id/tv_tip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#727998"
    android:textColorHighlight="@android:color/transparent"
    tools:text="@string/_tip" />

or disable focus

act262
  • 463
  • 4
  • 7
0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout android:id="@+id/LinearLayout02"
        android:layout_height="50px"
        android:layout_width="fill_parent"
        // Layout Click enable
        android:clickable="true"
        // Setting Highlight Option in background property
        android:background="@android:drawable/list_selector_background" />
    </LinearLayout>
</LinearLayout>
Plumillon Forge
  • 1,659
  • 1
  • 16
  • 31
  • see [the help](http://stackoverflow.com/editing-help#code-and-preformatted-text) for how to format code in the editor – McDowell Jun 15 '11 at 07:04
-1
textView.setText(myString);
Linkify.addLinks(textView,Linkify.ALL);

This works for me.