user1701840
user1701840

Reputation: 1112

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.

Upvotes: 8

Views: 10252

Answers (4)

ericktux
ericktux

Reputation: 1

works fine today 04-21-2024

package com.example.pruebas2

import android.os.Bundle
import android.text.Html
import android.text.method.LinkMovementMethod
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        var button1 = findViewById<Button>(R.id.button1)

        button1.setOnClickListener{
                val d: AlertDialog = AlertDialog.Builder(this)
                    //.setPositiveButton(R.string.ok, null)
                    .setPositiveButton("OK", null)
                    //.setIcon(R.drawable.icon)
                    .setTitle("About")
                    .setMessage(Html.fromHtml("Creado por Ericktux <br/>" +   // <br/> = saltar línea
                                                     "Visita mi web <br/>" +
                                                     "<a href=\"http://www.ericksystem.com\">Click aquí</a> <br/>" +
                                                     "Muchas gracias"))
                    .create()
                d.show()
                // Make the textview clickable. Must be called after show()
                // Make the textview clickable. Must be called after show()
                val textView: TextView? = d.findViewById(android.R.id.message)
                if (textView != null) {
                    textView.movementMethod = LinkMovementMethod.getInstance()
                }
        }
    }
}

Upvotes: 0

Adil Saiyad
Adil Saiyad

Reputation: 1592

AlertDialog.Builder builder1 = new AlertDialog.Builder(youractivity.this);

builder1.setMessage(Html.fromHtml("your message,<a href=\"http://www.google.com\">link</a>"));

builder1.setCancelable(false);
builder1.setPositiveButton("ok", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    }
});

AlertDialog Alert1 = builder1.create();
Alert1 .show();
((TextView)Alert1.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 3

Gray lin
Gray lin

Reputation: 33

You need to use all in HTML style

like this

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

Upvotes: 0

kabuko
kabuko

Reputation: 36302

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.

Upvotes: 5

Related Questions