Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RadioGroup with radio buttons in different layouts

Tags:

android

Is it possible to have a radio group with radio buttons in their own layouts? Every radio button has to be on a row that contains text and an image. I wouldn't use a list view because I only have 2 rows.

like image 665
Gratzi Avatar asked May 24 '11 11:05

Gratzi


People also ask

What is the difference between RadioGroup and radio button?

In Android, RadioButton are mainly used together in a RadioGroup. In RadioGroup checking the one radio button out of several radio button added in it will automatically unchecked all the others. It means at one time we can checked only one radio button from a group of radio buttons which belong to same radio group.

Can radio buttons have different names?

Radio buttons allow only one choice within a group of buttons. Each radio button within a group should have the same name. You can create more than one group of radio buttons by using different names.

Should radio buttons be horizontal or vertical?

Vertical positioning of radio buttons is safer. Try to lay out your lists vertically, with one choice per line. If you still need a horizontal layout with multiple options per line, make sure to space the buttons and labels so that it's absolutely clear which choice goes with which label.

How many radio buttons in a group of can be selected at the same time?

Only one radio button in a given group can be selected at the same time. Radio buttons are typically rendered as small circles, which are filled or highlighted when selected.


1 Answers

Here is the way to do it:

Create a separate RadioGroup on each RadioButton in the XML layout file. For example:

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

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

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

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

Now, inside the java source code, do something like this:

rdoFooWrapper = (RadioGroup) findViewById(R.id.rdoFooWrapper);
rdoFoo = (RadioButton) findViewById(R.id.rdoFoo);
rdoBooWrapper = (RadioGroup) findViewById(R.id.rdoBooWrapper);
rdoBoo = (RadioButton) findViewById(R.id.rdoBoo);

rdoFoo.setOnCheckedChangeListener(this); // implement OnCheckedChangeListener to the current class
rdoBoo.setOnCheckedChangeListener(this);

// ...

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
    rdoFooWrapper.clearCheck();
    rdoBooWrapper.clearCheck();
}
like image 148
Eng.Fouad Avatar answered Sep 17 '22 15:09

Eng.Fouad