androidlinkify

linkify on textview with domain in text not working, links not clickable


I'm not able to linkfy a string with a href when the string says the domain name with it. so string. let me show you.

Here is the string:

<string name="go_to_settings">Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status</string>

And it would say this to the user: "Please go to "Settings" in mywebsite.com to change this status. "

here is the textview I'm using:

  <TextView
      android:id="@+id/tv_instructions"
      android:layout_width="280dp"
      android:layout_height="wrap_content"
      android:layout_marginTop="73dp"
      android:lineSpacingExtra="3sp"
      android:textAlignment="center"
      android:textColor="#5c5c5c"
      android:textColorLink="@color/action_blue"
      android:linksClickable="true"
      android:text="@string/go_to_settings"/>

and here is how i am trying to make the link clickable:

 Linkify.addLinks(tv_instructions, Linkify.ALL)
 tv_instructions.movementMethod=LinkMovementMethod.getInstance()

But i am seeing happen on api 24 and 27 device and emulator is that the link is going to www.mywebsite.com instead of www.mywebsite/settings

but here is the strange thing, if i change the text the user sees and remove .com from that then it works fine. so if the user see this instead it works:

and it would say this to the user: "Please go to "Settings" in mywebsite to change this status. "

notice there is no .com being mentioned. How can i get this displayed. i also tried this way:

tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
tv_instructions.movementMethod=LinkMovementMethod.getInstance()

and textview like this:

    <TextView
                       android:id="@+id/tv_instructions"
                       android:layout_width="280dp"
                       android:layout_height="wrap_content"
                       android:layout_marginTop="73dp"
                       android:lineSpacingExtra="3sp"
                       android:textAlignment="center"
                       android:textColor="#5c5c5c"

                       android:text="@string/go_to_settings"
               />

you guys have any idea what I'm doing wrong ?


Solution

  • Figured this out. for some reason i needed to wrap my text as character data using tag cData in my strings file. this works:

    <string name="go_to_settings"><![CDATA[Please go to "Settings" in <a href="https://www.mywebsite.com/settings">mywebsite.com</a> to change this status :]]></string>
    

    then the textview looks like this:

     <TextView
          android:id="@+id/tv_instructions"
          android:layout_width="280dp"
          android:layout_height="wrap_content"
          android:layout_marginTop="73dp"
          android:lineSpacingExtra="3sp"
          android:text="@string/go_to_settings"
          android:textAlignment="center"
          android:textColor="#5c5c5c"
          android:textColorLink="@color/action_blue"/>
    

    Java Code

    tv_instructions.text=(utils.fromHTML(resources.getString(R.string.go_to_settings),null))
    tv_instructions.movementMethod=LinkMovementMethod.getInstance()