Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created December 19, 2023 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/c6ccc3889e8b86607300548ffccb3050 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/c6ccc3889e8b86607300548ffccb3050 to your computer and use it in GitHub Desktop.
Android Studio AlertDialog with HTML Link
class MyDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val html = "<p style=\"color:blue;\">テキストを入力します。</p>" +
"<p><ul><li>A</li><li>B</li><li>C</li></ul></p>" +
"<p>Google検索は<a href=\"http://google.com\">こちら</a></p>"
val dialog = activity?.let {
AlertDialog.Builder(it)
.setTitle("タイトル")
.setMessage(HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY))
.setPositiveButton("OK") { _, _ -> }
.create()
}
return dialog ?: throw IllegalStateException("アクティビティがNullです。")
}
override fun onStart() {
super.onStart()
dialog?.findViewById<TextView>(android.R.id.message)?.movementMethod = LinkMovementMethod.getInstance()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment