Search code examples
androidandroid-radiogroup

Issue in radiogroup while adding radiobuttons dynamically


I didn't see any post similar to my problem. I have a radiogroup which has two buttons as follows,

<RadioGroup
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/basic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/advanced"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </RadioGroup>

In my application,Sometimes I need one more button which I have added dynamically like,

button=new AppCompatRadioButton(Activity.this);                                                              
button.setId(R.id.my_custom_id);                                                                                  
radiogroup.addView(button);

Oncheckedchangelistener code for that radiogroup,

(radioGroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId== R.id.basic) {
                        button.setChecked(false);
                        //my code
                    }
                    else if(checkedId== R.id.advanced) {
                        button.setChecked(false);
                        //my code
                    }
                    else {
                       //it is newly added button's part
                    }
                }
            });

The problem is, when the newly added button is checked, it is not unchecked when I am clicking other button. I tried to solve this in OnCheckedChangeListener by setting button.setChecked(false); when other buttons were checked. But it doesn't work. I don't know which makes problem in that.

Can Anyone help me.Thanks!


Solution

  • Use your Activity class context while initializing the RadioButton.

    Instead of Activity.this use MainActivity.this

    Here is the Full working code..in your activity:

            RadioGroup group = (RadioGroup) findViewById(R.id.group);
    
            group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    Log.d("chk", "id" + checkedId);
    
                    if (checkedId == R.id.basic) {
                        //some code
                        Toast.makeText(MainActivity.this, "First", Toast.LENGTH_SHORT).show();
                    } else if (checkedId == R.id.advanced) {
                        //some code
                        Toast.makeText(MainActivity.this, "Second", Toast.LENGTH_SHORT).show();
                    } else if (checkedId == R.id.my_custom_id) {
                        //some code
                        Toast.makeText(MainActivity.this, "Third", Toast.LENGTH_SHORT).show();
                    }
    
                }
    
            });
            RadioButton button = new RadioButton(this);
            button.setId(R.id.my_custom_id);
            button.setText("Dynamic Button");
            group.addView(button);
            group.clearCheck();
    

    In your layout Xml:

    <RadioGroup
        android:id = "@+id/group"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/basic"
            android:layout_width="wrap_content"
            android:text="Basic"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/advanced"
            android:layout_width="wrap_content"
            android:text="Advanced"
            android:layout_height="wrap_content"
            />
    </RadioGroup>