Search code examples
androidkotlinandroid-studioandroid-radiogroupandroid-selector

Set Variable depending on RadioGroup button selected in Kotlin


I am currently trying to get my Android app to give me a variable based on which button in a RadioGroup I have clicked. As of right now, it is only returning either null or the default value I have set no matter how I try to do the Kotlin side of the coding. The group has five buttons, and each one represents a different number. I am trying to pass that number to the next activity as a variable, but since the variables are declared within if statements, the variable is not recognized when I am trying to send it to the next activity. What I have right now is:

                val companyName = findViewById<EditText>(R.id.companyNameText).text.toString()
                var hbv = "3"
                findViewById<RadioGroup>(R.id.hb).setOnCheckedChangeListener { _, checkedId ->
                    if(checkedId == R.id.hb1) {
                        var hbv = "1"
                    }
                    if(checkedId == R.id.hb2) {
                        var hbv = "2"
                    }
                    if(checkedId == R.id.hb3) {
                        var hbv = "3"
                    }
                    if(checkedId == R.id.hb4) {
                        var hbv = "4"
                    }
                    if(checkedId == R.id.hb5) {
                        var hbv = "5"
                    }
                }
                val intent = Intent(this, SecondActivity::class.java)
                intent.putExtra("companyName", companyName)
                intent.putExtra("hbv", hbv)

The companyName variable is taken from an EditText field and passes without any issues. I also have 3 of these radio groups, each containing 5 buttons, but they will all work the same once I get it working. Is there an easy way to set hbv within the if statement or a way to set it equal to the text of the radio button (1, 2, 3, 4, and 5 are the text attributes).


Solution

  • You are introducing a new local hbv variable within each if block instead of assigning a new value to the existing one. Change your code as follows:

    var hbv = "3"
    findViewById<RadioGroup>(R.id.hb).setOnCheckedChangeListener { _, checkedId ->
        if(checkedId == R.id.hb1) {
            hbv = "1"
        }
        if(checkedId == R.id.hb2) {
            hbv = "2"
        }
        if(checkedId == R.id.hb3) {
            hbv = "3"
        }
        if(checkedId == R.id.hb4) {
            hbv = "4"
        }
        if(checkedId == R.id.hb5) {
            hbv = "5"
        }
    }
    val intent = Intent(this, SecondActivity::class.java)
    intent.putExtra("companyName", companyName)
    intent.putExtra("hbv", hbv)
    

    If you create a var inside an if block, the variable only is scoped to that if block, so when you finally pass the number to the next activity, you are only passing the value of the outer hbv variable, which always is going to be 3.