'How do I get a string that contains a link in Android?
following problem: I have got a method that generates the result of some computation and returns this result as a string. Now I would like this result to contain a clickable link. How do I do that?
Here is what I have done so far:
This is part of my method that generates the result string:
Spanned link = Html.fromHtml("<a href='http://www.apple.com'>Apple.com</a>");
String result = "Website is: " + link;
Then, in another method that uses the String result I just set the text of the TextView resultText like this:
resultText.setText(result);
The problem is that while the text is displayed the text is not a link and hence not clickable. However, I already set this attribute in XML file of the according TextView:
android:linksClickable="true"
What is wrong here?
Solution 1:[1]
You can cut the string according to ' . You can do this trim() or substring() method.Take a look at this link.
Solution 2:[2]
See How to make normal links in TextView clickable? question.
<TextView
...
android:autoLink="web"/>
android:autoLink="web" controls whether links such as urls and email addresses are automatically found and converted to clickable links in TextView. Try and see.
Update:
In your original code try
Spanned link = Html.fromHtml("![CDATA[<a href="http://www.apple.com">Apple.com</a>]]");
or
declare a string in string.xml:
<string name="mylinks"><![CDATA[<a href="http://www.apple.com">Apple.com</a>]]></string>
and then
String link = Html.fromHtml(getResources().getString(R.string.mylinks));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | emreturka |
| Solution 2 | Community |
