user6868737
user6868737

Reputation: 137

How to remove the highlight color from hyperlink [Android]

I want to remove the highlight color when user clicked on the textview. So I'm able to change all the color and make it successful but it does not open the link when I clicked on textview. If I remove the AutoLink its worked...

So my question is how can i remove the highlight without using autolink?

XML file:

<TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Customer Service"
        android:textAlignment="center"
        android:textSize="15sp"
        android:clickable="true"
        android:linksClickable="true"
        android:autoLink="web"
        android:background="@drawable/about_us_selector"
        android:textColor="@color/about_us_color" />

Background drawable:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/light_gray"
        android:state_pressed="true"/>
</selector>

Color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
        android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

Java:

String webTv1 = "<a href='http://www.google.com'> Customer Service </a>";
    tv1 = (TextView) findViewById(R.id.tv1);
    tv1.setClickable(true);
    tv1.setText(Html.fromHtml(webTv1));
    //set user name in blue color and remove underline from the textview
    Spannable spannedTv1 = Spannable.Factory.getInstance().newSpannable(
            Html.fromHtml(webTv1));
    Spannable processedText = removeUnderlines(spannedTv1);
    if (tv1 != null) {
        tv1.setText(processedText);
        tv1.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

public static Spannable removeUnderlines(Spannable p_Text) {
    URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = p_Text.getSpanStart(span);
        int end = p_Text.getSpanEnd(span);
        p_Text.removeSpan(span);
        span = new URLSpanNoUnderline(span.getURL());
        p_Text.setSpan(span, start, end, 0);
    }
    return p_Text;
}

Upvotes: 2

Views: 3652

Answers (1)

user6868737
user6868737

Reputation: 137

Finally, I managed to get the highlight removed:

Add another color/highlight.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#00ffffff"/>
</selector>

Then, define the color at your xml file:

android:textColorHighlight="@color/hightlight.xml"

Before that, remove the autoLink from the XML file...

Upvotes: 7

Related Questions