Android-Is it possible to add a clickable link into a string resource
Just use an HTML format link in your resource:
<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>
You can then use setMovementMethod(LinkMovementMethod.getInstance())
on your TextView
to make the link clickable.
There is also TextView
's android:autoLink
attribute which should also work.
I found something interesting. Let me know if any of you observed this.
Below hyperlink is not working if you use
android:autoLink="web"
but works with
TextView link = (TextView) findViewById(R.id.link);link.setMovementMethod(LinkMovementMethod.getInstance());<string name="my_link"> <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> Click me! </a></string>
but if you use the following link it works with both
android:autoLink="web" (or)TextView link = (TextView) findViewById(R.id.link);link.setMovementMethod(LinkMovementMethod.getInstance());<string name="my_link"> <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource </a></string>
As answered by @Nikolay Elenkov In Kotlin I used it from string resources in this way (detailed way for freshers):
in my layout place a checkbox:
<CheckBox android:id="@+id/termsCB" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginHorizontal="@dimen/spacing_normal" android:layout_marginTop="@dimen/spacing_normal" android:text="@string/terms_and_conditions" />
in strings.xml
<string name="terms_and_conditions">I read and accept the <a href="http://www.spiffyshow.com/">Terms and Conditions</a></string>
in my activity
class inside the onCreate() method:
termsCB.movementMethod = LinkMovementMethod.getInstance()