Search code examples
androidandroid-layoutradio-buttonradio-group

RadioButtons inside same RadioGroup but both buttons can be selected


I have two RadioButtons inside the same RadioGroup. But beside each RadioButton i want an ImageButton (info). Therefore my code is as below:

<RadioGroup>
    <LinearLayout>     //Horizontal 
        <RadioButton/>
        <ImageButton/>
    </LinearLayout>
    <LinearLayout>     //Horizontal 
        <RadioButton/>
        <ImageButton/>
    </LinearLayout>
<RadioGroup/>

But now the problem I am facing is fairly simple, both RadioButtons can be selected. I want only one to get selected at a time.

Plz ignore any typos. Bound to post this from phone.


Solution

  • Well I did find a solution which worked for me because I had very few RadioButtons.

    I set an OnClickListener() for each RadioButton and set other unselected.

    final RadioButton a1 = (RadioButton) findViewById(R.id.a1);
    final RadioButton a2 = (RadioButton) findViewById(R.id.a2);
        a1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a2.setChecked(false);
            }
        });
        a2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a1.setChecked(false);
            }
        });