Home > other >  AlertDialog hyperlink not clickable
AlertDialog hyperlink not clickable

Time:10-24

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

    val termsDialog = AlertDialog.Builder(this@MainActivity)
    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

CodePudding user response:

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()
  • Related