Skip to content Skip to sidebar Skip to footer

How To Align Radiobuttons In A Radiogroup

I have a radiogroup in which I have 4 radiobuttons in it.I want them to be align as a two horizontal lines i.e two radiobuttons horizontally and other two radiobuttons horizontally

Solution 1:

Here is another simple solution.

main_activity.xml

<LinearLayoutxmlns: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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/one_radio_btn"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="0.5"android:text="one" /><RadioButtonandroid:id="@+id/two_radio_btn"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="0.5"android:text="two" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/three_radio_btn"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="0.5"android:text="three" /><RadioButtonandroid: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

publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener {

  private RadioButton mOneRadioButton;
  private RadioButton mTwoRadioButton;
  private RadioButton mThreeRadioButton;
  private RadioButton mFourRadioButton;

@OverrideprotectedvoidonCreate(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);

}

@OverridepublicvoidonClick(View v) {
    intid= 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 :

<RadioGroupandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"><RadioButtonandroid:id="@+id/radioButton1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1" /><RadioButtonandroid:id="@+id/radioButton2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:orientation="horizontal"><RadioButtonandroid:id="@+id/radioButton3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1" /><RadioButtonandroid: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"?><TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tableLayout1"android:layout_width="fill_parent"android:layout_height="fill_parent" ><RadioGroupandroid: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 --><TableRowandroid:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="5dip" ><RadioButtonandroid: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" /><RadioButtonandroid: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&apos;ve done Business Together"android:textSize="15sp" /></TableRow><TableRowandroid:id="@+id/tableRow1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:padding="5dip" ><RadioButtonandroid: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" /><RadioButtonandroid: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

  1. create RadioGroup with android:orientation = "horizontal"
  2. put 2 radio buttons in it.
  3. create another RadioGroup same as above.
  4. make sure Id of RadioGroups and RadioButtons are distinct.
  5. use setOnCheckedChangeListener to set same OnCheckedChangeListener on both the groups.

    //assuming activity implements OnCheckedChangeListener rg1.setOnCheckedChangeListener(this); rg2.setOnCheckedChangeListener(this);

  6. call clearCheck on other radio group (group 1 if check has been performed in group 2)

  7. check both groups for a checked RadioButton while performing an operation .

Post a Comment for "How To Align Radiobuttons In A Radiogroup"