Changing color of hyperlink in android

Viewed 3789

My objective is to change the hyperlink from a green color to a blue color in android. Currently the whole textview is green.

enter image description here

I'm using this class to remove the underlining of the hyperlink in android - I found it on SO:

public class TextViewNoUnderline extends AppCompatTextView {
    public TextViewNoUnderline(Context context) {
        this(context, null);
    }

    public TextViewNoUnderline(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public TextViewNoUnderline(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setSpannableFactory(Factory.getInstance());
    }

    private static class Factory extends Spannable.Factory {
        private final static Factory sInstance = new Factory();

        public static Factory getInstance() {
            return sInstance;
        }

        @Override
        public Spannable newSpannable(CharSequence source) {
            return new SpannableNoUnderline(source);
        }
    }

    private static class SpannableNoUnderline extends SpannableString {
        public SpannableNoUnderline(CharSequence source) {
            super(source);
        }

        @Override
        public void setSpan(Object what, int start, int end, int flags) {
            if (what instanceof URLSpan) {
                what = new UrlSpanNoUnderline((URLSpan) what);
            }
            super.setSpan(what, start, end, flags);
        }
    }
}

Then in my xml I did this:

<com.myapp.customshapes.TextViewNoUnderline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tos"
    tools:text="@string/signing_in"/>

Where signing_in is this string:

<string name="signing_in">&lt;body link=&quot;blue&quot;&gt;By signing in, you agree to our &lt;a href=&quot;com.myapp://http://www.myapp.com/Terms.html&quot;&gt;Terms of Service&lt;/a&gt;
    and &lt;a href=&quot;com.myapp://http://www.myapp.com/Privacy.html&quot;&gt;Privacy Policy&lt;/a&gt;</string>

I have set this in my java class:

    tos.setText(Html.fromHtml(getString(R.string.signing_in)));

As you can see, in my html, I said: <body link="blue">

The whole TOS which includes non-hyperlinked words and hyperlinked words is green as in the pic. Why are the hyperlinks not turning blue?

1 Answers
Related