androidandroid-studioradio-buttonradio-groupandroid-viewbinding

How to get text of the checked id from radio group using view binding?


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?


Solution

  • 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();