My question is about Android/Kotlin. By clicking on button, there must be checking if the inputs of EditText are filled in. If not filled in, there should be a message in Toast about the error.
Here is my code:
button.setOnClickListener {
try {
// конвертируем данные из textedit в Int при клике на кнопку
val minimumResult = Integer.parseInt(minimumInput.text.toString())
val maximumResult = Integer.parseInt(maximumInput.text.toString())
val result = (minimumResult..maximumResult).random()
resultLabel.text = result.toString()
val minToString = minimumInput.text.toString()
val maxToString = maximumInput.text.toString()
Log.d("testlogs", "$minToString и $maxToString");
if(minToString.isEmpty() && maxToString.isEmpty()) {
Log.d("testlogs","EditText inputs are not filled in")
Toast.makeText(applicationContext, "Please enter some message! ", Toast.LENGTH_SHORT).show()
}
if (minToString.isNotEmpty() && maxToString.isNotEmpty()){
Log.d("testlogs","Cool! That works! EditText inputs are filled in")
Toast.makeText(applicationContext, "Cool! That works! ", Toast.LENGTH_SHORT).show()
}
} catch (_: Exception) {
}
}
I do not know why the code "minToString.isEmpty() and maxToString.isEmpty()" does not check if the TextEdit filled in and does not display the error/or any message.
Can you tell, where is my mistake? Thank you!
Instead of isEmpty, I tried to use isBlank, match(""), minToString.length == 0, but anyway, when the inputs of TextEdit are empty (not filled in), Toast message with error does not appear.
Parsing of the int throws an exception when it's not a number, also when it's empty. Try switch it around so it first does the empty check, so like
button.setOnClickListener {
try {
val minToString = minimumInput.text.toString()
val maxToString = maximumInput.text.toString()
Log.d("testlogs", "$minToString и $maxToString");
if(minToString.isEmpty() && maxToString.isEmpty()) {
Log.d("testlogs","EditText inputs are not filled in")
Toast.makeText(applicationContext, "Please enter some message! ", Toast.LENGTH_SHORT).show()
}
if (minToString.isNotEmpty() && maxToString.isNotEmpty()){
Log.d("testlogs","Cool! That works! EditText inputs are filled in")
Toast.makeText(applicationContext, "Cool! That works! ", Toast.LENGTH_SHORT).show()
}
val minimumResult = Integer.parseInt(minimumInput.text.toString())
val maximumResult = Integer.parseInt(maximumInput.text.toString())
val result = (minimumResult..maximumResult).random()
resultLabel.text = result.toString()
} catch (_: Exception) {
}
}