How would I go about adding clickable links inside a ListView?
This is done using the autoLink attribute of a TextView. Took me some time to dig through the documentation so putting it here with an example in case someone else is looking for it:
Let us assume that you are binding your listview to a custom adapter. In that case, the following piece of code goes into your getView call:
Code:
textcontent.setText(Html.fromHtml(item.get_text()));
textcontent.setAutoLinkMask(Linkify.WEB_URLS);
Just put the link inside the text being passed to the setText call and you're done.
XML:
<TextView
android:id="@+id/txtview"
android:autoLink="web"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="put your link here"/>
Hope that helps...
If you have text that is already in HTML format, the best thing to do is the following:
TextView textcontent = (TextView) findViewById(...);
textcontent.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href="http://www.stackoverflow.com">stackoverflow.com</a>";
textcontent.setText(Html.fromHtml(text));
This will cause any link tags to be clickable in your text view. Alternately, you could use android:autoLink="web"
as suggested by Legend, but this has the side-effect of a) linkifying urls that are not wrapped in anchor tags, and b) potentially missing urls or linkifying things that aren't urls. If you want the smarts of autoLink then you should use it, but if all you want is to linkify the tags that are already there, you're better off using setMovementMethod()
.
See this bug report for more details: http://code.google.com/p/android/issues/detail?id=2219