Android TextView with Clickable Links: Open in Internal Web Browser?
<p>I have a TextView which is rendering basic HTML, containing 2+ links. I need to capture clicks on the links and open the links -- in my own internal WebView (not in the default browser.)</p>

<p>The most common method to handle link rendering seems to be like this:</p>

<pre><code>String str_links = "&lt;a href='http://google.com'&gt;Google&lt;/a&gt;&lt;br /&gt;&lt;a href='http://facebook.com'&gt;Facebook&lt;/a&gt;";
text_view.setLinksClickable(true);
text_view.setMovementMethod(LinkMovementMethod.getInstance());
text_view.setText( Html.fromHtml( str_links ) );
</code></pre>

<p>However, this causes the links to open in the default internal web browser (showing the "Complete Action Using..." dialog).</p>

<p>I tried implementing a onClickListener, which properly gets triggered when the link is clicked, but I don't know how to determine WHICH link was clicked...</p>

<pre><code>text_view.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {
        // what now...?
    }

});
</code></pre>

<p>Alternatively, I tried creating a custom LinkMovementMethod class and implementing onTouchEvent...</p>

<pre><code>public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
    String url = text.toString();
    // this doesn't work because the text is not necessarily a URL, or even a single link... 
    // eg, I don't know how to extract the clicked link from the greater paragraph of text
    return false;
}
</code></pre>

<p>Ideas?</p>

<p>Thanks!</p>

<hr>

<p>Based upon the accepted answer below, here's a function setTextViewHTML() which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.</p>

<pre><code>protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
          public void onClick(View view) {
              // Do something with span.getURL() to handle the link click...
          }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}

protected void setTextViewHTML(TextView text, String html)
{
    CharSequence sequence = Html.fromHtml(html);
        SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
        URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);   
        for(URLSpan span : urls) {
            makeLinkClickable(strBuilder, span);
        }
    text.setText(strBuilder);       
}
</code></pre>
