Simon Simon - 1 month ago 6
Android Question

Use string replace to change text color of URL link

What I mean is, is it possible to change the text URL link to the color green in a single string?

For example when I have a string which includes muliple URLs like below example. (Part 1) is it possible to set the text color of "http://information.com/qokp076wulpw" to green using string replace so it gets like in Part2?

Part 1:

<a class="postlink" href="http://test.site/i7xt1.htm">http://test.site/i7xt1.htm<br/></a><br/>Mirror:<br/><a class="postlink" href="http://information.com/qokp076wulpw">http://information.com/qokp076wulpw<br/></a>


Part 2:

<a class="postlink" href="http://information.com/qokp076wulpw"><font color='#1AB053'>http://information.com/qokp076wulpw</font><br/></a>

Answer

You can see this solution to enable links in a TextView from a HTML content and, to set link colors you could set the android:textColorLink attribute in XML layout to specify the links' color.

If you want only convert text URLs to links, is not necessary Java code. You can simply add this in your TextView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txt_no_html_tags"
    android:textColorLink="#00FF00"
    android:autoLink="web"
    android:linksClickable="true/>
Comments