disable button when edittext is empty kotlin
// Get references to your EditText and Button
val editText = findViewById<EditText>(R.id.editText)
val button = findViewById<Button>(R.id.button)
// Set a TextWatcher on the EditText
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
// Disable the button if the EditText is empty
button.isEnabled = !s.isNullOrEmpty()
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Not used
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// Not used
}
})
This code snippet sets up a TextWatcher for an EditText in Kotlin. It retrieves references to an EditText and a Button from the layout. Then, it adds a TextWatcher to the EditText to monitor text changes. Inside the afterTextChanged
method of the TextWatcher, it checks if the EditText is empty and disables the Button accordingly.