user-interface android:autolink="web" example - How can I get clickable hyperlinks in AlertDialog from a string resource?

7 Answers

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);
    Linkify.addLinks(s, Linkify.ALL);

    final AlertDialog d = new AlertDialog.Builder(activity)
        .setPositiveButton(android.R.string.ok, null)
        .setMessage( s )

    // Make the textview clickable. Must be called after show()
textview open browser

What I am trying to accomplish is to have clickable hyperlinks in the message text displayed by an AlertDialog. While the AlertDialog implementation happily underlines and colors any hyperlinks (defined using <a href="..."> in the string resource passed to Builder.setMessage) supplied the links do not become clickable.

The code I am currently using looks like this:

new AlertDialog.Builder(MainActivity.this).setTitle(
        .setPositiveButton(android.R.string.ok, null)

I'd like to avoid using a WebView to just display a text snippet.

Actually, if you want to simply use a string without dealing with all the views, the fastest way is to find message textview and linkify it:

d.setMessage("Insert your cool string with links and stuff here");
Linkify.addLinks((TextView) d.findViewById(, Linkify.ALL);

Instead of ...

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

... I now use:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
TextView textView = new TextView(this);

I was not really satisfied with the current answers. There are two things that are important when you want clickable hyperlinks in href style withing an AlertDialog:

  1. Set the content as View and not with setMessage(…), as only Views allow clickable HTML content
  2. Set the correct movement method (setMovementMethod(…))

Here is a working minimal example:


<string name="dialogContent">
    Cool Links:\n
    <a href="http://.com"></a>\n
    <a href="">Android Enthusiasts</a>\n

public void showCoolLinks(View view) {
   final TextView textView = new TextView(this);
   textview.setMovementMethod(LinkMovementMethod.getInstance()); // this is important to make the links clickable
   final AlertDialog alertDialog = new AlertDialog.Builder(this)
       .setPositivebutton("OK", null)

Simplest way:

final AlertDialog dlg = new AlertDialog.Builder(this)
                .setNeutralButton(R.string.close_button, null)
        // Important! will be available ONLY AFTER show()

If you are using a DialogFragment, this solution should help.

public class MyDialogFragment extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // dialog_text contains "This is a"
        String msg = getResources().getString(R.string.dialog_text);
        SpannableString spanMsg = new SpannableString(msg);
        Linkify.addLinks(spanMsg, Linkify.ALL);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            .setPositiveButton(R.string.ok, null);
        return builder.create();

    public void onStart() {

        // Make the dialog's TextView clickable

This is my solution. It creates a normal link without html tags involved and without any URL visible. It also keeps the design intact.

SpannableString s = new SpannableString("This is my link.");
s.setSpan(new URLSpan(""), 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

AlertDialog.Builder builder;
    builder = new AlertDialog.Builder(this,;
} else {
    builder = new AlertDialog.Builder(this);

final AlertDialog d = builder
        .setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Do nothing, just close
        .setNegativeButton("SHARE", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Share the app
                share("Subject", "Text");