I have a TextView with multiple ClickableSpans in it. When a ClickableSpan is pressed, I want it to change the color of its text.
I have tried setting a color state list as the textColorLink attribute of the TextView. This does not yield the desired result because this causes all the spans to change color when the user clicks anywhere on the TextView.
Interestingly, using textColorHighlight to change the background color works as expected: Clicking on a span changes only the background color of that span and clicking anywhere else in the TextView does nothing.
I have also tried setting ForegroundColorSpans with the same boundaries as the ClickableSpans where I pass the same color state list as above as the color resource. This doesn't work either. The spans always keep the color of the default state in the color state list and never enter the pressed state.
Does anyone know how to do this?
This is the color state list I used:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/pressed_color"/>
<item android:color="@color/normal_color"/>
</selector>
All these solutions are too much work.
Just set
android:textColorLink
in yourTextView
to some selector. Then create a clickableSpan with no need to override updateDrawState(...). All done.here a quick example:
In your
strings.xml
have a declared string like this:then in your activity:
and heres the important parts ..declare a selector like this calling it
myselector.xml
:And last in your
TextView
in xml do this:Now you can have a pressed state on your clickableSpan.
Place the java code as below :
In the above code specify wat color you want .
Change the style .xml as you want.
Try it out and say is this you want or something else . update me dude.
I finally found a solution that does everything I wanted. It is based on this answer.
This is my modified LinkMovementMethod that marks a span as pressed on the start of a touch event (MotionEvent.ACTION_DOWN) and unmarks it when the touch ends or when the touch location moves out of the span.
This needs to be applied to the TextView like so:
And this is the modified ClickableSpan that edits the draw state based on the pressed state set by the LinkTouchMovementMethod: (it also removes the underline from the links)
legr3c's answer helped me a lot. And I'd like to add a few remarks.
Remark #1.
I applied
LinkTouchMovementMethod
to aTextView
with two spans. The spans were highlighted with blue when clicked them.myTextView.setHighlightColor(getResources().getColor(android.R.color.transparent));
fixed the bug.Remark #2.
Don't forget to get colors from resources when passing
normalTextColor
,pressedTextColor
, andpressedBackgroundColor
.Should pass resolved color instead of resource id here
try this custom ClickableSpan:
and this SpanWatcher:
test it in onCreate:
This is my solution if you got many click elements (we need an interface): The Interface:
The class who manage the event:
In main class: