Android TextView With Custom Link Text

This is just going to be a quick how-to on creating a TextView with custom URL text.

To do this, you need to create a Spannable text object by using Html.fromHtml and then setting the MovementMethod of the TextView to a LinkMovementMethod. Here is an example using a dialog.

1
2
3
4
5
6
7
8
9
10
11
12
dialog = new Dialog(this);dialog.setContentView(R.layout.about);
dialog.setTitle(R.string.about_score_it);
TextView website = (TextView)dialog.findViewById(R.id.about_website);

String realURL = "http://blog.devminded.com/projects/score-it";
String visibleURL = "http://blog.devminded.com";

//The following builds the spannable item and will cause the text to display as a link
website.setText(Html.fromHtml("<a href=\" + realURL + "\">" + visibleURL + "</a>"));

//The following makes it clickable
website.setMovementMethod(LinkMovementMethod.getInstance());

Or with resources:

1
2
3
4
5
6
7
8
dialog = new Dialog(this);
dialog.setContentView(R.layout.about);
dialog.setTitle(R.string.about_score_it);
TextView website = (TextView)dialog.findViewById(R.id.about_website);

website.setText(Html.fromHtml("<a href=\"" + mRes.getString(R.string.about_score_it_website_url) + "\">" + mRes.getString(R.string.about_score_it_website_text) + "</a>"));

website.setMovementMethod(LinkMovementMethod.getInstance());

To see the full context of this example please check out ScoreIt.java

Copyright © 2013 Thomas Holmes