How To Align Radiobuttons In A Radiogroup
Solution 1:
Here is another simple solution.
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/one_radio_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="one" />
<RadioButton
android:id="@+id/two_radio_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="two" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/three_radio_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="three" />
<RadioButton
android:id="@+id/four_radio_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="four" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RadioButton mOneRadioButton;
private RadioButton mTwoRadioButton;
private RadioButton mThreeRadioButton;
private RadioButton mFourRadioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOneRadioButton = (RadioButton) findViewById(R.id.one_radio_btn);
mTwoRadioButton = (RadioButton) findViewById(R.id.two_radio_btn);
mThreeRadioButton = (RadioButton) findViewById(R.id.three_radio_btn);
mFourRadioButton = (RadioButton) findViewById(R.id.four_radio_btn);
mOneRadioButton.setOnClickListener(this);
mTwoRadioButton.setOnClickListener(this);
mThreeRadioButton.setOnClickListener(this);
mFourRadioButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.one_radio_btn:
mOneRadioButton.setChecked(true);
mTwoRadioButton.setChecked(false);
mThreeRadioButton.setChecked(false);
mFourRadioButton.setChecked(false);
break;
case R.id.two_radio_btn:
mOneRadioButton.setChecked(false);
mTwoRadioButton.setChecked(true);
mThreeRadioButton.setChecked(false);
mFourRadioButton.setChecked(false);
break;
case R.id.three_radio_btn:
mOneRadioButton.setChecked(false);
mTwoRadioButton.setChecked(false);
mThreeRadioButton.setChecked(true);
mFourRadioButton.setChecked(false);
break;
case R.id.four_radio_btn:
mOneRadioButton.setChecked(true);
mTwoRadioButton.setChecked(false);
mThreeRadioButton.setChecked(false);
mFourRadioButton.setChecked(true);
break;
}
}
}
Solution 2:
RadioGroup is subclass of LinearLayout,if you want it to support multiline,then need to overwrite it,but now has such a widget can use:MultiLineRadioGroup,help the sample could help you.
Solution 3:
Try it with two different LinearLayout :
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RadioGroup>
Solution 4:
Just try this and tell me if it fits the requirement.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RadioGroup
android:id="@+id/radiogroup_relation_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/HeadingText"
android:background="#4269c6" >
<!-- 2 columns -->
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<RadioButton
android:id="@+id/radiobutton_friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:button="@drawable/custom_radiobutton"
android:padding="5dp"
android:paddingLeft="10dp"
android:text="Friend"
android:textSize="15sp" />
<RadioButton
android:id="@+id/radiobutton_business"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:button="@drawable/custom_radiobutton"
android:padding="5dp"
android:paddingLeft="10dp"
android:text="We've done Business Together"
android:textSize="15sp" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip" >
<RadioButton
android:id="@+id/radiobutton_colleague"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:button="@drawable/custom_radiobutton"
android:padding="5dp"
android:paddingLeft="10dp"
android:text="Colleague"
android:textSize="15sp" />
<RadioButton
android:id="@+id/radiobutton_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:button="@drawable/custom_radiobutton"
android:padding="5dp"
android:paddingLeft="10dp"
android:text="Other"
android:textSize="15sp" />
</TableRow>
</RadioGroup>
</TableLayout>
I have used table layout here, so that there are 2 rows of RadioButtons.
Solution 5:
you can achieve this by using 2 radioGroups
- create
RadioGroup
withandroid:orientation = "horizontal"
- put 2 radio buttons in it.
- create another
RadioGroup
same as above. - make sure Id of
RadioGroups
andRadioButtons
are distinct. use
setOnCheckedChangeListener
to set sameOnCheckedChangeListener
on both the groups.//assuming activity implements OnCheckedChangeListener rg1.setOnCheckedChangeListener(this); rg2.setOnCheckedChangeListener(this);
call
clearCheck
on other radio group (group 1 if check has been performed in group 2)- check both groups for a checked
RadioButton
while performing an operation .
Post a Comment for "How To Align Radiobuttons In A Radiogroup"