1

I want to make like below image .

enter image description here

I was try to design using linear layout but its network in radio group. In radio group have just two orientation vertical or horizontal so how can I achieve like this .

Its should be in same radio group because its option of same Question.

thanks in advance.

Vishal Thakkar
  • 2,117
  • 2
  • 16
  • 33
  • you have to use 2 different radio group and handle it in code to make sure that only one radio button from both group can select – Khizar Hayat May 03 '16 at 09:38
  • You can look into [this link](http://stackoverflow.com/a/43735636/2715073) answered by me in another post. – Pankaj May 02 '17 at 10:31
  • You can look into [this link](http://stackoverflow.com/a/43735636/2715073) answered by me in another post. – Pankaj May 02 '17 at 10:32

2 Answers2

3

Try this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical" >

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

    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />


</RadioGroup>

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radio3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />


</RadioGroup>

</LinearLayout>

Java code :

private OnCheckedChangeListener listener1 = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {
            m_group2.setOnCheckedChangeListener(null); // remove the listener before clearing so we don't throw that stackoverflow exception(like Vladimir Volodin pointed out)
            m_group2.clearCheck(); // clear the second RadioGroup!
            m_group2.setOnCheckedChangeListener(listener2); //reset the listener
           // Log.e("XXX2", "do the work");

            int chkId1 = m_group1.getCheckedRadioButtonId();
            int chkId2 = m_group2.getCheckedRadioButtonId();
            int realCheck = chkId1 == -1 ? chkId2 : chkId1;

            RadioButton rb = (RadioButton)findViewById(realCheck);
            selected_mode = rb.getText().toString();
            Log.i("Selected_Mode", "" + selected_mode);
        }
    }
};

private OnCheckedChangeListener listener2 = new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1) {
            m_group1.setOnCheckedChangeListener(null);
            m_group1.clearCheck();
            m_group1.setOnCheckedChangeListener(listener1);
            //Log.e("XXX2", "do the work");

            int chkId1 = m_group1.getCheckedRadioButtonId();
            int chkId2 = m_group2.getCheckedRadioButtonId();
            int realCheck = chkId1 == -1 ? chkId2 : chkId1;

            RadioButton rb = (RadioButton)findViewById(realCheck);
            selected_mode = rb.getText().toString();
            Log.i("Selected_Mode", "" + selected_mode);
        }
    }
};

call the listener to OnCreate()

m_group1.setOnCheckedChangeListener(listener1);
m_group2.setOnCheckedChangeListener(listener2);
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
-1
  1. declare a field of type int "mCurrentId" that hold checked radioButton id
  2. declare an int array to hold all the radioButtons ids.
  3. place your radioButtons however you want in your xml but give each an id.

in the YourActivity.onCreate:

setUpRadioButtons();

in YourActivity:

  //fields
  int[] myRadioButtons;
  int currentCheckedRadioButton = 0;

  //setUpRadioButtons method
  private void setUpRadioButtons() {
    myRadioButtons= new int[6];
    myRadioButtons[0] = R.id.first;
    myRadioButtons[1] = R.id.second;
    //..
    for (int radioButtonID : myRadioButtons) {
        findViewById(radioButtonID).setOnClickListener(
                    new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentCheckedRadioButton != 0)
                    ((RadioButton) findViewById(currentCheckedRadioButton)).setChecked(false);
                currentCheckedRadioButton = v.getId();

            }
        });
    }
}
med.Hamdan
  • 190
  • 1
  • 6