RadioButtons inside the same RadioGroup, but both buttons can be selected

I have two RadioButtonsinside the same RadioGroup. But next to everyone RadioButtonI want ImageButton(info). So my code is as follows:

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

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

Plz ignore any typos. Connected to publish this from the phone.

+4
source share
5 answers

Well, I found a solution that worked for me, because I had very few RadioButtons.

OnClickListener() RadioButton .

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);
        }
    });
0

RadioGroup LinearLayout RadioButton . .

, , , RadioButton LinearLayout.

2 : RadioGroup,
extend RadioButton , , RadioButton.

+1

RadioGroup - ViewGroup . drawableRight.

0
  • If you do not want this, use my code and delete the radio group, you can use custom radio buttons

      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);                    
          a1.setChecked(true);
    
      }
         });
       a2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        a1.setChecked(false);
        a2.setChecked(true);
    
    }
    });
    
0
source

UPDATED: I think this will help you. I had the same problem.

<LinearLayout> //Horizontal
    <LinearLayout>//Vertical
        <ImageView/>
        <ImageView/>
    </LinearLayout>
    <LinearLayout>//Vertical
        <RadioGroup> //Vertical
            <RadioButton/>
            <RadioButton/>
        </RadioGroup>
    </LinearLayout>
</LinearLayout>
-1
source

Source: https://habr.com/ru/post/1622586/


All Articles