user1801060
user1801060

Reputation: 2821

Typechecking in Kotlin

I am a Kotlin newbie. I have a simple app in which I'm trying to sanitise all my input. Everything works in theory, but my app doesn't produce the required output.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val editTextWeight = findViewById<EditText>(R.id.weightEditText) as EditText
        val editTextHeight = findViewById<EditText>(R.id.heightEditText) as EditText

        val calculateButton = findViewById<Button>(R.id.calcButton)
        calculateButton.isEnabled = false

        val weight = editTextWeight.text.toString().toDoubleOrNull()
        val height = editTextHeight.text.toString().toDoubleOrNull()

        if (weight != null && height != null ) {
            calculateButton.isEnabled = true

            calculateButton?.setOnClickListener()
            {
                val bmi = weight / (height*height )
                Toast.makeText(this@MainActivity,
                        "Your BMI is $bmi", Toast.LENGTH_LONG).show()
            }
        }

    }
} 

I have tried to add an else condition ie:

    //calculateButton.isEnabled = false
    if (weight != null && height != null ) {
        //calculateButton.isEnabled = true

        calculateButton?.setOnClickListener()
        {
            val bmi = weight / (height*height )
            Toast.makeText(this@MainActivity,
                    "Your BMI is $bmi", Toast.LENGTH_LONG).show()
        }
    }else
        Toast.makeText(this@MainActivity,
                "Please enter both values correctly!", Toast.LENGTH_LONG).show()

The button is clickable, but I get no output from the app.

What am I doing wrong? Is there an app state like in Angular where I can seperate component initialisation from program logic?

Upvotes: 0

Views: 51

Answers (1)

i30mb1
i30mb1

Reputation: 4776

You should check the text input when you perform click on your button, in perfect programm your code should look like this :

class MainActivity : AppCompatActivity() {
    lateinit var calculateButton: Button
    lateinit var editTextWeight: EditText
    lateinit var editTextHeight: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        editTextWeight = findViewById(R.id.weightEditText)
        editTextHeight = findViewById(R.id.heightEditText)
        calculateButton = findViewById(R.id.calcButton)

        calculateButton.setOnClickListener()
        {
            val weight: Double = editTextWeight.text.toString().toDoubleOrNull() ?: 0.0
            val height: Double = editTextHeight.text.toString().toDoubleOrNull() ?: 0.0
            val bmi = weight / (height * height)

            if (bmi.isNaN())
                Toast.makeText(this@MainActivity,
                        "Input error, please try again!", Toast.LENGTH_LONG).show()
            else
                Toast.makeText(this@MainActivity,
                        "Your BMI is $bmi", Toast.LENGTH_LONG).show()

        }
    }
}

Upvotes: 1

Related Questions