Android Dialog for EditText (Kotlin)
July 24, 2018EditTextDialog
- 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
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn