Monday, August 27, 2012

How to Use Android's TextView as a Hyperlink

With a few simple pieces of code, it's really easy to convert a TextView into a hyperlink, which opens the default browser outside your app.

Just follow these steps:
  1. Set the text of the TextView with a bit of HTML (by using the Html.fromHTML method, or by using the strings.xml file)
    • "<a href=\"http://www.programmingmobile.com\">ProMobile</a>"
  2. Set the MovementMethod of the TextView with LinkMovementMethod.getInstance()
That's it! It is important to note that this method does cause the link to open outside the control of your application. If you want the link to open within your app, you should open the link within a WebView instead.

No comments:

Post a Comment