0

I am using the "setMovementMethod" to make clickable an url on a TextView. Better look the follow code:

1. String value = "By signing up, you agree to our <a href=\"https://app.mywebsite.com/terms\">My App Term</a> and confirm that you have read our <a href=\"https://app.mywebsite.com/privacypolicy\">Privacy Policy</a>";

2. TextView text = (TextView) findViewById(R.id.text);

3. text.setText(Html.fromHtml(value));

4. text.setMovementMethod(LinkMovementMethod.getInstance());

The problem is about the slash just after ".com" of the url. If I remove that slash and I write the url like that https://app.mywebsite.com then it works perfectly but when I write the url like that https://app.mywebsite.com/terms then the link isn't clickable. I can see the link highlighted but when click on the link then it does not work

How I could resolve this? Thank you very much.

Alex
  • 480
  • 1
  • 5
  • 15

2 Answers2

0

Firstly,

String value = "By signing up, you agree to our <a href="https://app.mywebsite.com/terms">My App Term</a> and confirm that you have read our <a href="https://app.mywebsite.com/privacypolicy">Privacy Policy</a>";

is illegal because - you are not allowed to use double-quote i.e. " inside another double-quote.

So, the correct form should be:

String value = "By signing up, you agree to our <a href='https://app.mywebsite.com/terms'>My App Term</a> and confirm that you have read our <a href='https://app.mywebsite.com/privacypolicy'>Privacy Policy</a>";

When I do this change, I am able to click the link, or else, the code will not even compile.

Secondly, have a check on the Build version and use the two parameter version of the Html.fromHtml(string, int)

More information on it is here:

Priyabrata
  • 1,202
  • 3
  • 19
  • 57
  • Sorry. I escaped (\\) every double-quote in the original code but here can't be see it. I changed my code like you are saying (even with a single quote) but It doesn't work yet (it works if I remove the slash) if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ textview.setText(Html.fromHtml(value, Build.VERSION.SDK_INT)); } else { textview.setText(Html.fromHtml(value)); } – Alex Nov 06 '19 at 09:24
  • which version of android are you testing it in? I tried in Pie and 10, seems to work. – Priyabrata Nov 06 '19 at 09:42
  • Hi Priyabrata . I am testing on Android 7 and Android 10. Only work when I remove the slash on the url. – Alex Nov 06 '19 at 09:48
  • I have a feeling the issue is related to something else. Will it be possible to replicate the issue and putting it in git? I am not able to reproduce the issue. – Priyabrata Nov 06 '19 at 09:59
  • 1
    I found the mistake @Priyabrata. We have configured url to the app on Google Play console. When you click in an url with our domain the app opens. When we clicked on the link nothing happened because we were already on the app. I found the mistake creating a new project. I figured out it when I clicked the url on the new app and my original app was opened instead of the browser. It was my fault. Sorry everybody. I am going to try this library https://github.com/saket/Better-Link-Movement-Method to capture the url and to be able to open it. – Alex Nov 07 '19 at 10:06
  • glad you figured it out!! @Alex – Priyabrata Nov 07 '19 at 10:08
0

Create a function look like:

fun applyHtmlToTextView(tv: TextView?, html: String) {
    if (tv == null)
        return

    tv.movementMethod = LinkMovementMethod.getInstance()

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        tv.text = Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
    } else {
        tv.text = Html.fromHtml(html)
    }
}

Add your string to the string.xml file as below:

<string name="lb_your_string"><![CDATA[<a href="https://google.com”>Google.</a>]]></string>

And using it by adding the code:

applyHtmlToTextView(tv, getString(R.string.lb_your_string))

OR you can edit the value variable to:

String value = "By signing up, you agree to our <a href=https://app.mywebsite.com/terms>My App Term</a> and confirm that you have read our <a href=https://app.mywebsite.com/privacypolicy>Privacy Policy</a>"

Removed \"

John Le
  • 1,116
  • 9
  • 12
  • Sorry but I get the text (value String) from a server. I can't use the string.xml file. – Alex Nov 06 '19 at 09:38
  • @Alex: I updated my answer. You can ask the back-end guys for changing the message. I hope it helps – John Le Nov 06 '19 at 09:49
  • Thanks @John Le but I tried to remove \" from backend but It only worked with the url without slash. – Alex Nov 06 '19 at 09:56