ClickableSpan - How to remove color on text when added?
Asked Answered
P

2

7

I've used ClickableSpan on a TextView. Upon adding the span, the color of text where it has been applied was changed too.

Checking SO questions, what I've seen are changed color after it was clicked. In my case, the color is already different upon rendering the view.

How can I remove the color from the ClickableSpan?

Panache answered 18/5, 2017 at 16:52 Comment(1)
You can avoid using clickable span and just make TextView is clickable or using borderless button could be a good approachRizas
A
12

Clickable span have updateDrawState(TextPaint ds) method. set same color as you text color for clickable span also. so it will look same (2nd Approch)

@Override public void updateDrawState(TextPaint ds) {
    //super.updateDrawState(ds);
    ds.setColor(linkColor);
    ds.setUnderlineText(false); // set to false to remove underline
}
Apprentice answered 18/5, 2017 at 18:6 Comment(0)
J
0

If you don't want to specify the colour manually, you can simply "undo" the ds.color overwrite that ClickableSpan performs. This will make the span use the existing text colour.

private class MyClickableSpan(
    ...
) : ClickableSpan() {
    
    override fun updateDrawState(ds: TextPaint) {
        val colour = ds.color
        super.updateDrawState(ds)
        ds.color = colour
    }

    ...
}

Justiciable answered 6/9 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.