How can I get clickable hyperlinks in AlertDialog from a string resource?

I didn’t really like the currently most popular answer because it significantly changes the formatting of the message in the dialog.

Here’s a solution that will linkify your dialog text without otherwise changing the text styling:

    // Linkify the message
    final SpannableString s = new SpannableString(msg); // msg should have url to enable clicking
    Linkify.addLinks(s, Linkify.ALL);

    final AlertDialog d = new AlertDialog.Builder(activity)
        .setPositiveButton(android.R.string.ok, null)
        .setIcon(R.drawable.icon)
        .setMessage( s )
        .create();

    d.show();

    // Make the textview clickable. Must be called after show()
    ((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

Leave a Comment