AlertDialog hyperlink not clickable

Issue

I have a alertdialog that contains a hyperlink, however the hyperlink isn’t clickable. It does show as a hyperlink

    val termsDialog = AlertDialog.Builder([email protected])
    val termsView = layoutInflater.inflate(R.layout.termsdialog, null)
    val termsBox: CheckBox = termsView.findViewById(R.id.termsCheckbox)
    val termsMsg = Html.fromHtml("By using this application you accept to our Terms and Conditions and Privacy Policy. \nMore information <a href=\"https://mylink.com\">here</a>")

    termsDialog.setTitle("Terms and Conditions")
    termsDialog.setView(termsView)
    termsDialog.setMessage(termsMsg)
    termsDialog.setPositiveButton("OK") { _, _ -> }
    termsDialog.setCancelable(false)

    termsBox.setOnCheckedChangeListener { compoundButton, _ ->
        if (compoundButton.isChecked) {
            storeDialogStatus(true)
        } else {
            storeDialogStatus(false)
        }
    }

    // Automatic show terms dialog when dialog status is not checked
    if (!this.getDialogStatus()) {
        termsDialog.show()
    }

The word here is a hyperlink

Solution

Create a TextView for your message in termsdialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/dialog_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Then you must call setMovementMethod(LinkMovementMethod.getInstance()) on your TextView:

val termsDialog = AlertDialog.Builder(context)
val termsView = layoutInflater.inflate(R.layout.termsdialog, null)
val termsMessage: TextView = termsView.findViewById(R.id.dialog_textview)
val termsMsg = Html.fromHtml("By using this application you accept to our Terms and Conditions and Privacy Policy. \nMore information <a href=\"https://mylink.com\">here</a>")

termsDialog.setTitle("Terms and Conditions")
termsDialog.setView(termsView)
termsMessage.setText(termsMsg)
termsMessage.setMovementMethod(LinkMovementMethod.getInstance())
termsDialog.show()

Answered By – Behnam Maboudi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published