Skip to content

Instantly share code, notes, and snippets.

@shahharshil0
Last active April 28, 2021 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shahharshil0/f9bd345f7243a6bb556f9a3a4639ff27 to your computer and use it in GitHub Desktop.
Save shahharshil0/f9bd345f7243a6bb556f9a3a4639ff27 to your computer and use it in GitHub Desktop.
fun TextInputEditText.isNotNullOrEmpty(errorString: String): Boolean {
val textInputLayout = this.parent.parent as TextInputLayout
textInputLayout.errorIconDrawable = null
this.onChange { textInputLayout.error = null }
return if (this.text.toString().trim().isEmpty()) {
textInputLayout.error = errorString
false
} else {
true
}
}
fun EditText.isNotNullOrEmpty(errorString: String): Boolean {
this.onChange { this.error = null }
return if (this.text.toString().trim().isBlank()) {
this.error = errorString
false
} else {
true
}
}
fun EditText.onChange(cb: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
cb(s.toString())
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment