Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created December 21, 2023 13:53
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/2a8b3954df0f41186c78e8c3384d4cf3 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/2a8b3954df0f41186c78e8c3384d4cf3 to your computer and use it in GitHub Desktop.
Android Studio AlertDialog with ImageView
class MyDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = activity?.let {
// カスタムレイアウトの用意
val inflater = it.layoutInflater
val customDialogView = inflater.inflate(R.layout.custom_dialog, null)
// タイトル
val title = customDialogView.findViewById<TextView>(R.id.title)
title.text = "こんにちは!"
// 画像名
val imageName = customDialogView.findViewById<TextView>(R.id.image_name)
imageName.text = "アイコン画像"
// メッセージ
val message = customDialogView.findViewById<TextView>(R.id.message)
message.text = "ここにメッセージを入力します。ここにメッセージを入力します。ここにメッセージを入力します。"
AlertDialog.Builder(it)
.setView(customDialogView)
.setPositiveButton("OK") { _, _ -> }
.create()
}
return dialog ?: throw IllegalStateException("アクティビティがNullです。")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment