I am collecting values from two edittext fields on button click. I've implemented handling for all of the necessary conditions (negative numbers, too big value, zero), but the case with empty edittext keeps crashing, despite handling that case also.
fun buildAlertDialog(message : String = "Bad input value")
{
val alertDialogBuilder = AlertDialog.Builder(this@MainActivity)
alertDialogBuilder.setTitle("Error")
alertDialogBuilder.setMessage("$message")
alertDialogBuilder.setNeutralButton("Close"){_,_ ->}
val alertDialog : AlertDialog = alertDialogBuilder.create()
alertDialog.show()
}
btnCalculate.setOnClickListener{
var heightInput = editTxt_EnterHeight.text.toString().toDouble()
var weightInput = editTxt_EnterWeight.text.toString().toInt()
if(editTxt_EnterHeight.toString().trim().isNotEmpty())
{
Toast.makeText(this, "Height is not empty", Toast.LENGTH_SHORT).show()
if(heightInput > 2.5)
{
buildAlertDialog("Bad input value - too tall")
}
else if(heightInput == 0.0)
{
buildAlertDialog("Bad input value - height value can't be zero")
}
else if(heightInput < 0)
{
buildAlertDialog("Bad input value - height can't be a negative number")
}
}
else if(editTxt_EnterHeight.text.toString().trim().length == 0)
{
buildAlertDialog("Plese enter height value")
}
if(editTxt_EnterWeight.toString().trim().isNotEmpty())
{
Toast.makeText(this, "Weight is not empty", Toast.LENGTH_SHORT).show()
if(weightInput > 350)
{
buildAlertDialog("Bad input value - too big weight value")
}
else if(weightInput == 0)
{
buildAlertDialog("Bad input value - weight can't be zero")
}
else if(weightInput < 0)
{
buildAlertDialog("Bad input value - weight can't be a negative number")
}
}
else if(editTxt_EnterWeight.text.toString().trim().length == 0)
{
buildAlertDialog("Please enter weight value")
}
}
Height edittext inputType is set as numberDecimal, while weight edittext is set as number.
I've tried implementing multiple solutions I found on stackoverflow and other sites, but all of them had the same result. (Check if EditText is empty kotlin android, https://www.tutorialspoint.com/how-to-check-if-android-edittext-is-empty, https://inducesmile.com/kotlin-source-code/how-to-check-if-edittext-is-empty-in-kotlin/)