1

I'm trying to display JSON results in a textview (that resides within a listview). One of the Results is a URL which has to be displayed as "View results". I'm using the following code to display the URL as "View Results":

        String result = "<a href=\"" + jsonObject.get("url") + "\">" + getString(R.string.hyperlink_text) + "</a>" + "\n";
        bbieResults.put("Result", Html.fromHtml(result));

The related xml layout:

<TextView
    android:id="@+id/list_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/list_label"
    android:layout_marginLeft="10dp"
    android:autoLink="web"
    android:linksClickable="true"
    android:textSize="25dp" />

This textview does display "View results" as a label for the URL but I can't actually click it. So how can I make this a clickable hyperlink?

Thanks in advance :)

Bart
  • 769
  • 2
  • 13
  • 29

3 Answers3

13
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(Html.fromHtml(str));
Jave
  • 31,598
  • 14
  • 77
  • 90
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • Thanks, I've tested this in another activity and this is indeed what I was looking for. However in this particular case it results in a nullpointer exception. The textview is located in a custom row.xml which is used by a listview. Could you point me in the right direction as to how I would be able to fix that? – Bart Feb 21 '12 at 16:09
0
android:autoLink="web"
android:linksClickable="true"

This worked for me when the textview was inside a .xml file and inside a listview.

patrickfdsouza
  • 747
  • 6
  • 17
0

Your solution is here. https://github.com/saket/Better-Link-Movement-Method

In Gradle file:

    implementation 'me.saket:better-link-movement-method:1.1'

In Kotlin file:

message?.message?.let {
  chatMessageTextView.setText(
     HtmlCompat.fromHtml(
         it.trim(),
         HtmlCompat.FROM_HTML_MODE_LEGACY
     ),
     TextView.BufferType.SPANNABLE
   )
}
    
chatMessageTextView.movementMethod =
    BetterLinkMovementMethod.newInstance().apply {
        setOnLinkClickListener { _, url ->
            // Handle click or return false to let the framework handle this link.
             handleMessageLink(itemView.context,url)
             true
        }
    }

In Layout file:

             <TextView
                    android:id="@+id/chat_message_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/files_layout"
                    android:layout_marginStart="@dimen/space_ultra_small"
                    android:text="@string/text_small"
                    android:textColor="@color/black"
                    android:padding="@dimen/space_ultra_small"
                    android:textColorLink="@color/sky_blue"
                    android:textSize="@dimen/text_size_large" />
acarlstein
  • 1,799
  • 2
  • 13
  • 21