Android: Make your TextView look like a hyperlink

There's no shortage of posts regarding Linkify and automatically making part of the TextView clickable. Just search for "android textview hyperlink" and you'll get a fair idea.

TextView tvMessage = (TextView) findViewById(R.id.txtMsg);
tvMessage.setAutoLinkMask(Linkify.ALL);

And the result would be something like...

image

But when you don't want the text to include http:// is when you'll have to look elsewhere.

image

To get this, it's really simple. You can even make a quick TextView subclass out of it.

/**
* Sets a hyperlink style to the textview.
*/
public static void makeTextViewHyperlink(TextView tv) {
SpannableStringBuilder ssb = new SpannableStringBuilder();
ssb.append(tv.getText());
ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(ssb, TextView.BufferType.SPANNABLE);
}

And to set up the click handler, just use an ordinary OnClickListener so you can handle the click any way you want.

TextView tv = (TextView) findViewById(R.id.linkRecipes);
Utils.makeTextViewHyperlink(tv);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), RecipeActivity.class);
startActivity(intent);
}
});

ibEtB0r2aWAFJ
Done, like a BOSS!

While you're at it, check out my Diablo II Runewords app!

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog