Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created December 21, 2023 00:30
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/5f22a8a2cd0470bf29123260414917d1 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/5f22a8a2cd0470bf29123260414917d1 to your computer and use it in GitHub Desktop.
Android Studio AlertDialog Customize - Kotlin
class MyDialogFragment : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// タイトル
val titleView = TextView(activity)
titleView.text = "タイトル"
titleView.textSize = 24f
titleView.setTextColor(Color.WHITE)
titleView.setBackgroundColor(resources.getColor(R.color.blue))
titleView.setPadding(20, 20, 20, 20)
titleView.gravity = Gravity.CENTER
// メッセージ
val msgView = TextView(activity)
msgView.text = "ここにメッセージを入力します。ここにメッセージを入力します。ここにメッセージを入力します。"
msgView.textSize = 16f
msgView.setTextColor(Color.BLACK)
msgView.setPadding(20, 20, 20, 40)
val dialog = activity?.let {
AlertDialog.Builder(it)
.setCustomTitle(titleView)
.setView(msgView)
.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