Search code examples
androidandroid-alertdialog

How to have text and clickable URL link on Alertdialog?


I read sample codes and tried something below, but it doesn't work. The text didn't appear to be a hyperlink. Please help:(

String randomString = XXXX.getString();
        if(randomString .contains("XXXX"))
        {
            TextView tv  = new TextView(this);
            tv.setMovementMethod(LinkMovementMethod.getInstance());
            tv.setText(randomString +"/n"+Html.fromHtml("<a href=https://play.google.com/store/apps/details?id=com.xxxxxxxxx>Click Here</a>"));
            AlertDialog dialog = new AlertDialog.Builder(activity.this)
            .setView(tv)
            .setPositiveButton("OK!", new DialogInterface.OnClickListener()
            {

                public void onClick(DialogInterface dialog, int which)
                {

                }
            })
            .show();

        }

EDIT: This work:

tv.setText(Html.fromHtml("<br><a href=https://play.google.com/store/apps/details?id=com.xxxxxxxxx>Click Here</a>"));

without the randomString+

As soon as I put the randomString+Html.fromHtml.... The "Click Here" become a regular text

But I would like to put the randomString inside the textview as well.


Solution

  • You're missing quotes around your URL. Try:

    Html.fromHtml("<a href='https://play.google.com/store/apps/details?id=com.xxxxxxxxx'>Click Here</a>")
    

    Note the single quotes around the URL.