4

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.

Srujan Barai
  • 2,295
  • 4
  • 29
  • 52

6 Answers6

1

RadioGroups are LinearLayouts themselves and only support RadioButtons as direct children. You can see this behavior here.

What you are trying to do is not possible like this, since you wrap your RadioButton in a LinearLayout.

You have 2 possibilities: You can either copy the code and make your own RadioGroup,
or you can extend RadioButton and just change its appearence by applying your custom layout there, inside your RadioButton.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
0

RadioGroup does not allow any ViewGroup as its child . You will have to use drawableRight attribute.

0

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);
        }
    });
Srujan Barai
  • 2,295
  • 4
  • 29
  • 52
  • I'll think I'll use this same approach. But since I have a lot of options, what I'll do is track the last radioButton selected and within OnClickListener() implement `lastRadioButtonSelected.setChecked(false)`. I guess have to strong-arm Android SDK to get stuff done on time ! – Someone Somewhere Feb 28 '18 at 00:50
0
  1. you can use radiobuttons only the child of a radio group
  2. if u dont want this then use my code and remove radiogroup u 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);
    
    }
    });
    
Sunil
  • 3,785
  • 1
  • 32
  • 43
0

RadioButton rbOne, rbTwo, rbThree, rbFour;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rbOne = findViewById(R.id.rbOne);
    rbTwo = findViewById(R.id.rbTwo);
    rbThree = findViewById(R.id.rbThree);
    rbFour = findViewById(R.id.rbFour);

    rbOne.setOnClickListener(this);
    rbTwo.setOnClickListener(this);
    rbThree.setOnClickListener(this);
    rbFour.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == rbOne) {
        isChecked(1);
        Toast.makeText(getApplicationContext(), rbOne.getText().toString(), Toast.LENGTH_SHORT).show();
    } else if (view == rbTwo) {
        isChecked(2);
        Toast.makeText(getApplicationContext(), rbTwo.getText().toString(), Toast.LENGTH_SHORT).show();
    } else if (view == rbThree) {
        isChecked(3);
        Toast.makeText(getApplicationContext(), rbThree.getText().toString(), Toast.LENGTH_SHORT).show();
    } else if (view == rbFour) {
        isChecked(4);
        Toast.makeText(getApplicationContext(), rbFour.getText().toString(), Toast.LENGTH_SHORT).show();
    }
}

private void isChecked(int position) {
    switch (position) {
        case 1:
            rbTwo.setChecked(false);
            rbThree.setChecked(false);
            rbFour.setChecked(false);
            break;
        case 2:
            rbOne.setChecked(false);
            rbThree.setChecked(false);
            rbFour.setChecked(false);
            break;
        case 3:
            rbOne.setChecked(false);
            rbTwo.setChecked(false);
            rbFour.setChecked(false);
            break;
        case 4:
            rbOne.setChecked(false);
            rbTwo.setChecked(false);
            rbThree.setChecked(false);
            break;
    }
}

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One Time"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/rbTwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="80dp"
            android:text="One Time"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rbThree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="One Time"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/rbFour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="80dp"
            android:text="One Time"
            android:textSize="16sp" />
    </LinearLayout>

</RadioGroup>
-1

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

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

1 [This is how it will appear in the end]

Dimitri Leite
  • 1,791
  • 13
  • 16
  • I'm not 100% sure if this helps because you don't show how to put an `ImageButton` beside this `RadioButtons` – cldrr Feb 01 '18 at 16:07
  • You don't need the `//Vertical` just replace that with the RadioGroup since a RadioGroup is already a layout. However, I imagine this approach is a PITA to line-up the radio buttons and images – Someone Somewhere Feb 28 '18 at 00:53
  • I re-updated the Answer matching your needs. Can you please reconsider the upvote? This way when you click on radiobutton, because they are under radiogroup, it will swap the clicked variable. – Dimitri Leite Nov 22 '18 at 22:43