0

So I have a RadioButton in the RadioGroup that should be checked without resorting to using Id.

This my XML code:

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

Here I check the selected RadioButton:

RadioGroup radioGroup = findViewById(R.id.radioGroup);
int radioButtonId = radioGroup.getCheckedRadioButtonId();

switch (radioButtonId) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomething();
        break;
}

I tried it:

1) Added attribute android:android:checkedButton to RadioGroup.

<RadioGroup
    android:id="@+id/radioGroup"
    android:checkedButton="@id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

doSometheing() is not executed because radioButtonId was not an index, but Id.

2) Then I added an attribute android:checked as "true" to my RadioButton.

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="RadioButton 1" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton 2" />
</RadioGroup>

doSometheing() was executed, but now it was possible to checked both RadioButtons at the same time. The first RadioButton could not be unchecked. radioButtonId remained constant == 1.

I hope that you will tell me the correct way.

Thank you. Have a nice day.

bjs22538
  • 3
  • 1
  • Why you don't want to provide `id` for `radio button`? Every component should have an `id`. – Abu Noman Aug 29 '19 at 18:05
  • This could help you: https://stackoverflow.com/questions/9842516/set-selected-index-of-an-android-radiogroup – Brainnovo Aug 29 '19 at 18:10
  • @AbuNoman, Most likely, this is the right approach. But I am afraid to get confused in Ids when there will be a lot of them. I prefer indexes. Tell me if I am deeply mistaken. – bjs22538 Aug 29 '19 at 18:11

2 Answers2

0

Use the findviewbyid:

radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
radiobutton1.setChecked(true);
ekbrothers
  • 54
  • 6
0

You can get index of checked RadioButton as follows:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

switch (idx) {
    case 1:
        doSomething();
        break;
    case 2:
        doSomething();
        break;
}

Note: Don't forget to assign id for the radio buttons.

Abu Noman
  • 435
  • 3
  • 13