Android Text Input Dialog With Inflated View (Kotlin)
March 7, 2018fun showCreateCategoryDialog() {
val context = this
val builder = AlertDialog.Builder(context)
builder.setTitle("New Category")
// https://stackoverflow.com/questions/10695103/creating-custom-alertdialog-what-is-the-root-view
// Seems ok to inflate view with null rootView
val view = layoutInflater.inflate(R.layout.dialog_new_category, null)
val categoryEditText = view.findViewById(R.id.categoryEditText) as EditText
builder.setView(view);
// set up the ok button
builder.setPositiveButton(android.R.string.ok) { dialog, p1 ->
val newCategory = categoryEditText.text
var isValid = true
if (newCategory.isBlank()) {
categoryEditText.error = getString(R.string.validation_empty)
isValid = false
}
if (isValid) {
// do something
}
if (isValid) {
dialog.dismiss()
}
}
builder.setNegativeButton(android.R.string.cancel) { dialog, p1 ->
dialog.cancel()
}
builder.show();
}
Content of R.layout.dialog_new_category
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/dialog_margin"
android:orientation="vertical"
>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<android.support.design.widget.TextInputEditText
android:id="@+id/categoryEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
- 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