Using Html.fromHtml (), you will get already formatted HTML text from this line.
For your case, this will lead to a βnew link,β but the following βhrefβ is ignored by the warning dialog because it does not display HTML. Therefore, the linker does nothing on your code. Try commenting on this: the behavior will persist.
Using Linkifier you can turn arbitrary text patterns (RegExes) into interactive links. You can add, for example, a string matching your regular expression to the predefined "base" content URI. Thus, it works not only for websites, but also for every URI of content used on Android. For more information, see Google DevSite .
You can use Linkifier without regular expressions, there are some standard templates. This is a mapping of email addresses, phone numbers, map or website coordinates. So for your solution to work:
- Set spannable String to regular URL (no HTML content)
- Just add links for the type "WEB_URLS" (Linkify.WEB_URLS) (Linkify.ALL will "search" for all predefined templates, as described above).
And it will work.
I adjusted your code, now in AlertWindow the URL "www.google.com" should be clickable:
final SpannableString s = new SpannableString("www.google.com"); final TextView tx1 = new TextView(this); tx1.setText(getString(R.string.librarytextpart1) + s + getString(R.string.librarytextpart2)); tx1.setAutoLinkMask(RESULT_OK); tx1.setMovementMethod(LinkMovementMethod.getInstance()); Linkify.addLinks(s, Linkify.WEB_URLS); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(getString(R.string.library)) .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }) .setView(tx1).show();
source share