AlertDialog with urls and text

I am trying to show in Android AlertDialog text and some words that can be clicked while opening a web page. I am doing something wrong with this because it does not open the link that I tell him, and the text of the hole can be clicked, and I try to avoid .... This is my code:

final SpannableString s = new SpannableString( Html.fromHtml("<br><a href=\"http://google.com\">a new link</a>") ); 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.ALL); 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(); 

When you click on the SpannableString dialog in the dialog box, it shows that all the text can be clicked ... and still does not open the web page. Of course, I do not understand anything from the documentation, but I do not understand that. Can you help please

+4
source share
1 answer

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(); 
+3
source

Source: https://habr.com/ru/post/1398141/


All Articles