Html <a> tag not working in Android textview
Asked Answered
G

3

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 :)

Glim answered 21/2, 2012 at 15:37 Comment(0)
V
13
textview.setMovementMethod(LinkMovementMethod.getInstance());
textview.setText(Html.fromHtml(str));
Viewer answered 21/2, 2012 at 15:41 Comment(1)
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?Glim
D
0
android:autoLink="web"
android:linksClickable="true"

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

Diedra answered 9/6, 2014 at 11:3 Comment(0)
A
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" />
Aleta answered 6/5, 2021 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.