3

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?

Simon
  • 19,658
  • 27
  • 149
  • 217
  • You can use spannablestringbuilder insteaad of html text – Raghunandan Mar 12 '16 at 19:43
  • Can u please give me an example and if it work, I will accept your answer – Simon Mar 12 '16 at 19:45
  • http://stackoverflow.com/questions/7570239/android-linkify-text-spannable-text-in-single-text-view-as-like-twitter-twee.There are many similar ones on stackoveflow. I might have answered similar ones before but cannot remember the link – Raghunandan Mar 12 '16 at 19:47

1 Answers1

2

The UrlSpan just takes the link color from the accent color, so give your TextView a style with a blue accent color.

styles.xml:

    <style name="AppThemeBlueAccent" parent="AppTheme">
        <item name="colorAccent">@color/blue</item>
    </style>

your_layout.xml:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/AppThemeBlueAccent"
        ...
        />
kris larson
  • 30,387
  • 5
  • 62
  • 74