I've the following convenience method:
public static void createLink(TextView textView, String linkText, String linkHRef) {
String htmlLink = "<a href='%s'>%s</a>";
Spanned spanned = Html.fromHtml(String.format(htmlLink, linkHRef, linkText));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(ContextCompat.getColor(textView.getContext(), android.R.color.holo_green_light));
textView.setText(spanned, TextView.BufferType.SPANNABLE);
}
I'm using that method to create a clickable link inside a textview, given the textview itself, the text link and the url.
Now I would to show a "click effect" when the user tap on that text.
How can I achieve this?
Thank you very much!
UPDATE
I was able to obtain what I mean in this way:
public static void createLink(TextView textView, String linkText, String linkHRef, @ColorRes int linkColorStateList) {
String htmlLink = "<a href='%s'>%s</a>";
Spanned spanned = Html.fromHtml(String.format(htmlLink, linkHRef, linkText));
textView.setTextColor(ContextCompat.getColorStateList(textView.getContext(), linkColorStateList));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(spanned, TextView.BufferType.SPANNABLE);
}
And defining a color state list xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="@color/green" />
<item android:state_focused="true" android:state_pressed="true" android:color="@color/green_dark" />
<item android:state_focused="false" android:state_pressed="true" android:color="@color/green_dark" />
<item android:color="@color/green" />
</selector>
Now the color of the text contained in the TextView changes when I press on it.
The only things that I don't understand is why the TextView background color changes too, becoming bright blue. My code doesn't do that, I think. Anyone maybe knows that?