I’m working on an Android Studio activity in which I have to calculate area and perimeter. I used intent to pass and access variables that will be used in another activity where the computations will happen. What I want to happen is that when the user chooses or selects either the Perimeter or the Area radio buttons, the code with the specified calculations should run. The problem is that when I run my code, nothing displays following the user input. We are required to use view Binding in this activity so I’m not sure how I can use the isChecked() or isSelected() functions.
MainActivity.kt:
val radioGrp = binding.radioGroup.text
val radioBtn1 = binding.radioButton1.text
val radioBtn2 = binding.radioButton2.text
val length = binding.edtTxtLength.text.toString()
val width = binding.edtTxtWidth.text.toString()
intent.putExtra("rGroup", radioGrp)
intent.putExtra("Btn1", radioBtn1)
intent.putExtra("Btn2", radioBtn2)
intent.putExtra("Length", length)
intent.putExtra("Width", width)
Activity.kt:
val getRGroup = intent.getStringExtra("rGroup")
val getRBtn1 = intent.getStringExtra("Btn1")
val getRBtn2 = intent.getStringExtra("Btn2")
val getLength = intent.getStringExtra("Length")
val getWidth = intent.getStringExtra("Width")
val Area = getLength*getWidth
val Perimeter = 2*(getLength+getWidth)
if(getRGroup == getRBtn1)
{
binding.textArea.text = buildString{
append("Perimeternn")
append("Answer: $Perimeter")
}
}
else if (getRGroup == getRBtn2)
{
binding.textArea.text = buildString{
append("Areann")
append("Answer: $Area")
}
}
It should do the perimeter calculation based on the conditional statement if the user selects the radio button designated for it, and the same goes for the area calculation.