---
# System prepended metadata

title: Dialog
tags: [Android, Kotlin]

---

###### tags: `Android` `Kotlin`
# Dialog

## 自定義Dialog
```kotlin=
private var dialog: Dialog? = null

fun showCustom(activity: Activity, layout: Int, keyboard: Boolean = false, gravityPosition: Int = -1): View? {
    if(!activity.isDestroyed){
        dialog?.dismiss()

        dialog = AlertDialog.Builder(activity).create()
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE) //無標題
        dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent) //透明背景

        //設定 Dialog 位置 (ex: gravityPosition = Gravity.Bottom)
        if (gravityPosition != -1) {
            val wlp = dialog?.window?.attributes
            wlp?.gravity = gravityPosition
            wlp?.flags = wlp?.flags?.and(WindowManager.LayoutParams.FLAG_DIM_BEHIND.inv())
            dialog?.window?.attributes = wlp
        }

        dialog?.show()
        
        //若畫面有 EditText，則設定 keyboard = true，用來解決 dialog 焦點問題
        if(keyboard)
            dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

        val view = View.inflate(activity, layout, null) //取得客製化畫面
        dialog?.setContentView(view)
        return view
    }
    return null
}
```

==使用方式==
```kotlin=
private fun showEditDialog(uid: String, msg: String) {
    val mActivity = context as YouTubeActivity
    DialogManager.instance.showCustom(mActivity, R.layout.dialog_input, true, Gravity.BOTTOM)?.let {
        val tv_title = it.findViewById<TextView>(R.id.tv_title)
        val ed_message = it.findViewById<EditText>(R.id.ed_message)
        val tv_send = it.findViewById<TextView>(R.id.tv_send)
        val img_cancel = it.findViewById<ImageView>(R.id.img_cancel)

        tv_title.text = "編輯留言"
        ed_message.setText(msg)

        tv_send.setOnClickListener {
            val content = ed_message.text.toString()

            if (content.isEmpty() || content.isBlank())
                return@setOnClickListener

            val req = VideoEditMessageReq(videoID, uid, content)
            DataManager.instance.doVideoEditMessage(req)
            DialogManager.instance.cancelDialog()
        }

        img_cancel.setOnClickListener {
            DialogManager.instance.cancelDialog()
        }

        it.post {
            Method.showKeyBoard(mActivity, ed_message)
        }
    }
}
```

## 寬度滿屏Dialog
由於 Dialog 在 Build 的時候，會有系統預設的主題，這個主題會讓我們無法把 Dialog 的寬度填滿螢幕
所以要先制定一個主題，並在建立 Dialog 時給予自訂的主題

在 styles.xml 新增
```xml=
<style name="Theme_Dialog" parent="android:Theme.Holo.Dialog">
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>
</style>
```

建立 Dialog 時使用
```kotlin=
AlertDialog.Builder(activity, R.style.Theme_Dialog).create()
```

## 參考文章
[Dialog 位置設定](https://blog.csdn.net/Naide_S/article/details/80091191)
[寬度滿屏 Dialog](https://stackoverflow.com/questions/40756208/android-dialog-occupy-100-width/40756329)