Cao Dongping
Cao Dongping

Reputation: 989

How to change TextView link background on hover

I want to change my TextView link background colour while user pressed on links.

I have search the web and found nothing.

The result I want is like picture below. The text @GeniusVcsh is pressed and with a light blue background.

enter image description here

Any help would be very appreciate.

Upvotes: 1

Views: 1822

Answers (2)

Euporie
Euporie

Reputation: 1996

Try use selector:

First create selector.xml in res\drawable like this:

<?xml version="1.0" encoding="UTF-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/holo_blue_bright" android:state_pressed="true"/>

</selector>

then add android:background="@drawable/selector" and android:clickable="true" in the TextView:

<TextView
    ...
    android:background="@drawable/selector"
    android:clickable="true"/>

Hope it helps.

more reference

Upvotes: 2

Christian Abella
Christian Abella

Reputation: 5797

I have created a class derived from TextView to handle links and custom colouring of text just recently. To answer your question, use Html.fromHtml function and pass it with HTML's font tag with a color attribute.

TextView tvRedText;
...
tvRedText.setText(Html.fromHtml("<font color=\"#ff0000\">" + "This is a red text " + "</font>"));

Upvotes: 0

Related Questions