I have made a tiny Android-project to familiarize myself with user input validation.
The app just has an EditText-control for user input, a button "Compute Result" and a TextView, which shows the result. As input is an integer expected.
The essential part of the code:
buttonCompute.setOnClickListener {
val strInput = inputEditText.text.toString()
if (!TextUtils.isEmpty(strInput)) {
val intInput = strInput.toIntOrNull()
val result = intInput ? .times(10) ? : 0
resultTextView.text = result.toString()
} else {
Toast.makeText(applicationContext, "Please provide a number!",
Toast.LENGTH_LONG).show();
}
}
It works, but it's a lot of code for such a small tasks.
Is there a better way? Does someone know further tricks and helpers, to accomplish the validation easier?