2

I had gone through this and found a common answer i.e

        int selectedId = radioGroup.getCheckedRadioButtonId();

        radioButton = (RadioButton) findViewById(selectedId);

        Toast.makeText(MyAndroidAppActivity.this,
            radioButton.getText(), Toast.LENGTH_SHORT).show();

But how to get text using view binding?

2 Answers2

7

You could do this:

EDIT:

int id = binding.radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = binding.getRoot().findViewById(id);
//Then get the radio button's text
Toast.makeText(MyAndroidAppActivity.this,
        radioButton.getText(), Toast.LENGTH_SHORT).show();
AzureStar123
  • 196
  • 7
0

To find the text of a selected radio button in Kotlin for Android, you can follow these steps:

  1. Declare a reference to the radio group in your activity or fragment. For example, if your radio group has an ID of radioGroup, you can declare it as follows:
val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)

2.Set a listener on the radio group to detect when a radio button is selected. You can use the setOnCheckedChangeListener method to achieve this:

radioGroup.setOnCheckedChangeListener { group, checkedId ->
val selectedRadioButton = findViewById<RadioButton>(checkedId)
val selectedText = selectedRadioButton.text.toString()
// Use the selectedText as needed

}

  1. Inside the listener, retrieve the selected radio button using the provided checkedId parameter. Then, you can access the text of the selected radio button using the text property, and convert it to a string using .toString() The selectedText variable will contain the text of the selected radio button, which you can use as needed in your application.

Remember to replace R.id.radioGroup with the actual ID of your radio group in your layout file.