Android Dialog for EditText (Kotlin)

July 24, 2018

EditTextDialog

  • Support Title, Hint and Multiline
class EditTextDialog : DialogFragment() {
    companion object {
        private const val TAG = "EditTextDialog"

        private const val EXTRA_TITLE = "title"
        private const val EXTRA_HINT = "hint"
        private const val EXTRA_MULTILINE = "multiline"
        private const val EXTRA_TEXT = "text"

        fun newInstance(title: String? = null, hint: String? = null, text: String? = null, isMultiline: Boolean = false): EditTextDialog {
            val dialog = EditTextDialog()
            val args = Bundle().apply {
                putString(EXTRA_TITLE, title)
                putString(EXTRA_HINT, hint)
                putString(EXTRA_TEXT, text)
                putBoolean(EXTRA_MULTILINE, isMultiline)
            }
            dialog.arguments = args
            return dialog
        }
    }

    lateinit var editText: EditText
    var onOk: (() -> Unit)? = null
    var onCancel: (() -> Unit)? = null

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val title = arguments?.getString(EXTRA_TITLE)
        val hint = arguments?.getString(EXTRA_HINT)
        val text: String? = arguments?.getString(EXTRA_TEXT)
        val isMiltiline = arguments?.getBoolean(EXTRA_MULTILINE) ?: false

        // StackOverflowError
        // val view = layoutInflater.inflate(R.layout.dialog_edit_text, null)
        val view = activity!!.layoutInflater.inflate(R.layout.dialog_edit_text, null)

        editText = view.findViewById(R.id.editText)
        editText.hint = hint

        if (isMiltiline) {
            editText.minLines = 3
            editText.inputType = InputType.TYPE_TEXT_FLAG_MULTI_LINE or InputType.TYPE_TEXT_FLAG_CAP_SENTENCES or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
        }
        if (text != null) {
            // editText.setText(text)
            // editText.setSelection(text.length)
            editText.append(text)
        }

        val builder = AlertDialog.Builder(context!!)
                .setTitle(title)
                .setView(view)
                .setPositiveButton(android.R.string.ok) { _, _ ->
                    onOk?.invoke()
                }
                .setNegativeButton(android.R.string.cancel) { _, _ ->
                    onCancel?.invoke()
                }
        val dialog = builder.create()

        dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)

        return dialog
    }
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="-5dp"
    android:layout_marginRight="-5dp"
    android:paddingLeft="?dialogPreferredPadding"
    android:paddingRight="?dialogPreferredPadding"
    >

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        >

        <android.support.design.widget.TextInputEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:inputType="textCapSentences|textNoSuggestions"
            />

    </android.support.design.widget.TextInputLayout>
</FrameLayout>

Usage

val dialog = EditTextDialog.newInstance(text = item.name, hint = "Description", isMultiline = true)
dialog.onOk = {
    val text = dialog.editText.text
    // do something
}
dialog.show(supportFragmentManager, "editDescription")

NOTE: If you want to do validation, do look into Prevent Dialog Closed On Button Click

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.