0

I have textview like this:

<TextView
        android:id="@+id/tvComment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web" />

tvComment.setText(Html.fromHtml(message));

Here is my text:

<a href='myapp://293482'>Jack </a> http://google.com

The problem is that <a href> not clickable. If I remove android:autoLink="web", it is clickable but the link http://google.com does not work.
How can I make both of them clickable?

Kevin Cronly
  • 1,388
  • 1
  • 13
  • 18
ductran
  • 10,043
  • 19
  • 82
  • 165
  • If you want to have two links in one TextView, try using a SpannableString with ClickableSpans. Don't forget setMovementMethod(LinkMovementMethod.getInstance()); in your TextView. – Thommy Jun 30 '15 at 14:59
  • http://stackoverflow.com/questions/9982241/android-clickable-textview-how-to-make-multiple-click-regions-on-text-and-catch – Anil Meenugu Jun 30 '15 at 15:05
  • @Thommy but my text is dynamic, how can I set span for all links? – ductran Jun 30 '15 at 15:14
  • Then you need to Regex the links – Thommy Jul 01 '15 at 09:58

3 Answers3

9

Please try this:

XML

<TextView
    android:id="@+id/tvHello"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:linksClickable="true"
    />

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
{
    ...
    String html = "<a href='myapp://293482'>Jack </a> http://google.com";
    tvHello = (TextView) findViewById(R.id.tvHello);
    tvHello.setText(linkifyHtml(html, Linkify.ALL));
    tvHello.setMovementMethod(LinkMovementMethod.getInstance());
}

public static Spannable linkifyHtml(String html, int linkifyMask) {
    Spanned text = Html.fromHtml(html);
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);

    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, linkifyMask);

    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    return buffer;
}
nuuneoi
  • 1,788
  • 1
  • 17
  • 14
0

According to Linkify.addLinks javadoc:

... it also removes any existing URLSpans attached to the Spannable, to avoid problems if you call it repeatedly on the same text.

Here is Kotlin style based on @nuuneoi's answer:


private fun String.toSpannableWithHtmlAndAutoLink() =
    Html.fromHtml(this, Html.FROM_HTML_MODE_COMPACT).let { text ->
        text.toSpannable().apply {
            Linkify.addLinks(this, Linkify.ALL)
            text.getSpans<URLSpan>().forEach {
                setSpan(it, text.getSpanStart(it), text.getSpanEnd(it), text.getSpanFlags(it))
            }
        }
    }


@BindingAdapter("textWithLinks")
fun setTextWithLinks(view: TextView, message: String?) = view.apply {
    text = message?.toSpannableWithHtmlAndAutoLink()
    movementMethod = LinkMovementMethod.getInstance()
}
0
/**
 * This method is used to set clickable part in text
 * Don't forget to set android:textColorLink="@color/link" (click selector) and
 * android:textColorHighlight="@color/window_background" (background color while clicks)
 * in the TextView where you will use this.
 */
   
 

    fun TextView.makeLinks(text: Spanned, conditionMatch: Boolean, action: () -> Unit) {
            this.text = text
            if (conditionMatch) {
                val startIndex = text.lastIndexOf(Constants.COMMA)
                val endIndex = text.lastIndexOf(Constants.DOT_DELIMITER)
                this.movementMethod = LinkMovementMethod.getInstance()
                this.setText(text, TextView.BufferType.SPANNABLE)
                val clickableSpan: ClickableSpan = object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        action.invoke()
                    }
                }
                val mySpannable: Spannable? = this.text as Spannable?
                mySpannable?.setSpan(clickableSpan, startIndex + Constants.NumericConstants.TWO, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            }
Swati
  • 146
  • 1
  • 10