Friday, October 8, 2010

Making TextView clickable (http)

The problem: I had a TextView which I wanted to make clickable, and open a web browser.

1. Brute force solution: change the TextView style to blue & underline. Set the onClick event to open web browser.

2. The following code:

SpannableString s = SpannableString.valueOf(entryText);
URLSpan span = new URLSpan(urlText);
s.setSpan(span, 0, entryText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(s);
mTextView.setMovementMethod(LinkMovementMethod.getInstance());

No comments: