Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Part of TextView clickable with a link

I have an app with lots of text that is in strings.xml. Parts of that text are hyperlinks that have to be clickable.

Now, when I insert that text into TextView from the layout file it works, but when I try to insert that same text into the same TextView with tv.settext(getStrings...) it won't work.

Here is the code:

strings.xml:

<string name="some_text">
For you to see the article, you need to click <a href='http://www.google.com'>this link</a>
\n\n
after that etc...
</string>

Activity:

TextView txt = findViewById(R.id.link_tv);
txt.setMovementMethod(LinkMovementMethod.getInstance());
txt.setText(getString(R.string.some_text));

activity_main.xml:

<TextView
     android:id="@+id/link_tv"
     android:textColor="@color/privacy_policy_link_text_color"
     android:paddingLeft="@dimen/_5sdp"
     android:paddingRight="@dimen/_5sdp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

Any thoughts?

like image 940
filipst Avatar asked Oct 16 '22 05:10

filipst


1 Answers

check below point before testing

  • Check your link starts with http:// or https://
  • Place your String in CDATA tag (like <string name="some_text"><![CDATA[PLACE_YOUR_HTML_STRING_HERE]]></string>)
  • Check you have added internet Permission in your manifest.xml file <uses-permission android:name="android.permission.INTERNET"/>
  • then dynamically html text can be set using below code into textView
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    textView.setText(Html.fromHtml(getString(R.string.html_string), Html.FROM_HTML_MODE_COMPACT));
} else { 
    textView.setText(Html.fromHtml(getString(R.string.html_string)));
}
Linkify.addLinks(textView, Linkify.ALL);
textView.setMovementMethod(LinkMovementMethod.getInstance());
like image 115
Muhammad Muzammil Sharif Avatar answered Oct 20 '22 23:10

Muhammad Muzammil Sharif