1

How can I add a link to a dialog message?

I tried the following code, but the link doesn't do anything on onclick:

builder.setMessage(Html.fromHtml(
      "Click on the  " +
      "<a href=\"http:\\www.google.com\">link</a> " +
       "to download."));

It didn't work for //www.google.com either.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
erdomester
  • 11,789
  • 32
  • 132
  • 234

3 Answers3

25

Use the following code to convert your HTML markup (including clickable hyperlinks):

((TextView) new AlertDialog.Builder(this)
    .setTitle("Info")
    .setIcon(android.R.drawable.ic_dialog_info)
    .setMessage(Html.fromHtml("<p>Sample text, <a href=\"http://google.nl\">hyperlink</a>.</p>"))
    .show()
    // Need to be called after show(), in order to generate hyperlinks
    .findViewById(android.R.id.message))
    .setMovementMethod(LinkMovementMethod.getInstance());
Steffen Brem
  • 1,738
  • 18
  • 29
1

You need to use Linkify

final SpannableString m = new SpannableString(message);
Linkify.addLinks(m, Linkify.WEB_URLS);

    aDialog = new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setMessage(m)
            .setNeutralButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        doNeutralClick();
                    }
                }
            )
            .create();

    aDialog.show();

    ((TextView) aDialog.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

See this question.

Community
  • 1
  • 1
MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
0

Standard implementation of dialog doesn't allow you to do such fancy stuff. You need to create a custom dialog by setting a custom layout content to it. For example:

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.layout_dialog);

For more details, read this and this.

waqaslam
  • 67,549
  • 16
  • 165
  • 178