2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Kotlin] AlertDialogをカスタマイズする

Last updated at Posted at 2019-05-21

AlertBuilderでAlertDialogをカスタマイズします。(カスタムレイアウト作成した方がおすすめですが)

・標準のAlertDialog
Screen Shot 2019-05-17 at 17.51.55.png

・成果物
Screen Shot 2019-05-17 at 17.51.41.png

ソースコード

AlertDialogFragment.kt

// 省略

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    // ダイアログのタイトルのカスタマイズ
    val text = TextView(context)
    text.text = "Error"
    text.setPadding(0, 25, 0, 25)
    text.textSize = 20F
    text.textAlignment = View.TEXT_ALIGNMENT_CENTER
    text.setTextColor(resources.getColor(R.color.white, null))
    text.background = resources.getDrawable(R.color.green, null)

    val builder = activity?.let { AlertDialog.Builder(it) }
    
    // setTitleではなくsetCustomTitleを使う
    builder!!.setCustomTitle(text)
            .setMessage(message)
            .setPositiveButton(okButtonName, onOkClickListener)

    val dialog = builder.show()

    // OKボタンを中央揃えに
    val button = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
    button.gravity = Gravity.CENTER
    button.setTextColor(resources.getColor(R.color.colorWhite, null))
    button.setPadding(0, -30, 0, -30)
    button.background = resources.getDrawable(R.drawable.green_button, null)
    button.textSize = 15F

    val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 55)
    layoutParams.gravity = Gravity.CENTER
    layoutParams.marginStart = 250
    layoutParams.marginEnd = 250
    layoutParams.topMargin = 20
    layoutParams.bottomMargin = 15
    button.layoutParams = layoutParams

    this.isCancelable = false

    return dialog
}

二つ以上のボタンがある時

AlertDialogFragment.kt

// 省略

        builder!!.setCustomTitle(text)
                .setMessage(message)
                .setPositiveButton("OK",onOkClickListener)
                .setNegativeButton("CANCEL",onCancelClickListener)
                .setOnDismissListener(onDismissListener)

        val dialog = builder.show()

        val okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE)
        okButton.gravity = Gravity.CENTER
        okButton.setTextColor(resources.getColor(R.color.white, null))
        okButton.setPadding(70, -30, 70, -30)
        okButton.background = resources.getDrawable(R.drawable.green_button, null)
        okButton.textSize = 15F

        val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 55)
        layoutParams.gravity = Gravity.CENTER
        layoutParams.marginStart = 30
        layoutParams.marginEnd = 160
        layoutParams.topMargin = 20
        layoutParams.bottomMargin = 10
        okButton.layoutParams = layoutParams

        val cancelButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE)
        cancelButton.gravity = Gravity.CENTER
        cancelButton.setTextColor(resources.getColor(R.color.green, null))
        cancelButton.setPadding(50, -30, 50, -30)
        cancelButton.textSize = 15F

        val layoutParams2 = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 55)
        layoutParams2.gravity = Gravity.CENTER
        layoutParams2.marginStart = 60
        layoutParams2.marginEnd = 30
        layoutParams2.topMargin = 20
        layoutParams2.bottomMargin = 10
        cancelButton.layoutParams = layoutParams2

// 省略

成果物

Screen Shot 2019-05-17 at 17.51.49.png

ダイアログ表示方法

Fragmentでカスタムダイアログを表示したい時の例です。ご参考してください。

val dialog = AlertDialogFragment()
dialog.title = getString("Error")
dialog.message = getString("XXXXXXXXXXXXXXXXXX")
dialog.onOkClickListener = DialogInterface.OnClickListener { _, _ ->
  // OK押したらなんかする
dialog.show(childFragmentManager, "")
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?