Android-Is it possible to add a clickable link into a string resource

AndroidXmlHyperlinkAndroid Alertdialog

Android Problem Overview


I usually set up some kind of AlertDialog to fire off when a user first uses one of my apps and I explain how to use the app and give an overall introduction to what they just downloaded. I also usually load my strings from a strings.xml file.

What I want to do is make one of the words in my string resource clickable like a hyperlink on a web page. Basically you'd have an AlertDialog and within the string resource there would be a highlighted word or possibly just a web address that they could press. I suppose I could just add a button that would take them to the site but I just wanted to know if making a word in your string resource a clickable hyperlink was possible.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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>

Solution 3 - Android

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()

Solution 4 - Android

Android doesn't make strings that contain valid link clickable automatically. What you can do, is add custom view to your dialog and use WebView to show the alert message. In that case, you can store html in your resources and they will be clickable.

View alertDialogView = LayoutInflater.inflate(R.layout.alert_dialog_layout, null);

WebView myWebView = (WebView) alertDialogView.findViewById(R.id.dialogWebView);
myWebView.loadData("<a href=\"http://google.com\">Google!</a>", "text/html", "utf-8");
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(alertDialogView);

alert_dialog_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<WebView android:id="@+id/dialogWebView" android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

Solution 5 - Android

For me, the hyperlink always works well, when I:

  1. Add these two lines to the activity/fragment:
textView.setMovementMethod(LinkMovementMethod.getInstance())
textView.setText(Html.fromHtml(getString(R.string.link)))
  1. Do not add anything to the xml (like autoLink etc).
  2. Define link in strings.xml like

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames andresakisView Question on Stackoverflow
Solution 1 - AndroidNikolay ElenkovView Answer on Stackoverflow
Solution 2 - AndroidPrakashView Answer on Stackoverflow
Solution 3 - AndroidShailendra MaddaView Answer on Stackoverflow
Solution 4 - AndroidTomislav MarkovskiView Answer on Stackoverflow
Solution 5 - AndroidnanorusView Answer on Stackoverflow