Any ideas why this link does not work? It is suppo...
# android
j
Any ideas why this link does not work? It is supposed to open up a web-browser?'
Copy code
<TextView
            android:id="@+id/textView6"
            android:layout_width="273dp"
            android:layout_height="45dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="32dp"
            android:autoLink="web"
            android:fontFamily="@font/avenir_next_regular"
            android:text="@string/terms_and_conditions"
            android:textAlignment="center"
            android:textSize="14sp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />
Copy code
<string name="terms_and_conditions">By tapping Sign in, you agree to our <a href="<http://www.google.com>" target="_top">Terms</a>. Learn how we process your data in our Privacy Policy and Cookies Policy.</string>
b
Apply a movement method to the textview movementMethod = LinkMovementMethod.getInstance()
✔️ 1
r
Your best approach is to use Spannables in code. See https://stackoverflow.com/questions/12418279/android-textview-with-clickable-links-how-to-capture-clicks Also, I strongly recommend wrapping the HTML anchor element in your string resource within a CDATA block, so the XML parsing doesn't get confused by it.
There are a lot of other approaches described on stackoverflow, including using setLinkMovementMethod as @brandonmcansh suggests -- and that's a lot simpler and worth trying. I've just found that Spannables are a robust solution, if unpleasantly complex.
j
@brandonmcansh where should I set the LinkMovement?
I tried it on onViewCreated but does not work
@rhenley Where should I add setLinkMovementMethod?
It does not work like this
Copy code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        textView6.movementMethod = LinkMovementMethod.getInstance()
        textView6.linksClickable = true
    }
r
@joshua onViewCreated sounds fine. Try this:
Copy code
textView6.text( Html.fromHtml( R.string.terms_and_conditions ) );
textView6.setMovementMethod(LinkMovementMethod.getInstance());
and omit textView6.linksClickable = true. That's one solution proposed in the StackOverflow article linked above. But this is why I use Spannables. Lots of stackoverflow about movementMethod being flaky. Spannables are a pain, but they work.
j
@rhenley This works:
Copy code
textView6.text = Html.fromHtml(123.toString(),0)
        textView6.movementMethod = LinkMovementMethod.getInstance()
This does not work:
Copy code
textView6.text = Html.fromHtml(resources.getString(R.string.terms_and_conditions),0)
        textView6.movementMethod = LinkMovementMethod.getInstance()
v
The most accessible way to handle this is to set a click listener on the full text view to open the link. An inline link is a SUPER SMALL area to try to click on
r
@Joshua Try using getText, instead of getString. (Although if you put that CDATA section in the string resource, or escaped the angle brackets around the anchor link tags, getSting should work -- try logging what you get back. Also try using a literal string containing an anchor link instead of "123", e.g.
textView6.text = Html.fromHtml("This <a href="<https://www.google.com>">Google</a>)
. This test removes getText/getString complications temporarily. But @vgonda is right -- unless you really need rich text, make the whole string clickable.
(And whatever you do, don't put "Click here" as the link text! 😉 )
1
j
This did not work 😞
Copy code
textView6.text = Html.fromHtml("This <a href="<https://www.google.com>">Google</a>)
I think I found the solution for this..... Wow super confusing.. You either have to use
Copy code
android:autoLink="web"
in your layout OR:
Copy code
TextView link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
126 Views