0

I try to make a TextView as hyperlink. This one worked as expected :

content.text = "<a href=${args.article.url}>Content</a>".fromHtml()

But I get a lint warning regarding using string resources. But this one does now show TextView as hyperlink:

content.text = getString(R.string.content, args.article.url).fromHtml()

And this is String resource :

<string name="content"><a href="%s">Content</a></string>

Is there any solution to fix it using String resource?

@Suppress("DEPRECATION")
fun String.fromHtml() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    Html.fromHtml(this, Html.FROM_HTML_MODE_COMPACT)
} else {
    Html.fromHtml(this)
}
7
  • Use getText() instead of getString()/fromHtml(): stackoverflow.com/a/19311520/115145 Commented May 10, 2019 at 21:46
  • @CommonsWare, when I use getText(R.string.content, args.article.url).fromHtml() I receive an error under args.article.url Commented May 10, 2019 at 21:51
  • Oh, right, getText() doesn't support placeholders. You could switch to the single-parameter getText() and use TextUtils.expandTemplate() or TextUtils.replace(). Commented May 10, 2019 at 21:55
  • 1
    Yes, that was right. Thanks. You can share your answer and I mark it as accepted. Commented May 10, 2019 at 22:15
  • 1
    I recommend that you answer your own question, showing what you wound up with! Commented May 10, 2019 at 22:16

1 Answer 1

1

This is how to set text in TextView :

content.text = TextUtils.expandTemplate(getText(R.string.content), args.article.url).toString().fromHtml()

And this is how to set String resources :

<string name="content"><![CDATA[ <a href="$s">Content</a>]]></string>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.