2

I want to arrange radio buttons in table layout in multiple rows and columns, the controls are arranged using below code but could not associate in the same radiobutton group.

Can you please help, below is the code I tried..

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RadioGroup
            android:id="@+id/radioLang1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:dividerPadding="0dp"
            android:orientation="vertical" >

            <TableRow>

                <RadioButton
                    android:id="@+id/radioEnglish1"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:checked="true"
                    android:onClick="onRadioButtonClicked"
                    android:text="@string/lang1"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/radioTransliteration"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:checked="true"
                    android:onClick="onRadioButtonClicked"
                    android:text="Punjabi Transliteration"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />
            </TableRow>

            <TableRow>

                <RadioButton
                    android:id="@+id/radioPunjabi1"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:onClick="onRadioButtonClicked"
                    android:text="Punjabi Vyakhya(Meaning)"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />

                <RadioButton
                    android:id="@+id/radioHindi"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:onClick="onRadioButtonClicked"
                    android:text="Hindi Mein Padhiye"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />
            </TableRow>

            <RadioButton
                android:id="@+id/radioEnglishTranslation1"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:onClick="onRadioButtonClicked"
                android:text="@string/eng_translate"
                android:textColor="@android:color/black"
                android:textSize="12sp" />
        </RadioGroup>
    </TableLayout>

Thanks

Amandeep

log N
  • 925
  • 9
  • 33
Amandeep Singh
  • 3,754
  • 8
  • 51
  • 72
  • You may want to have a look at the link below: http://stackoverflow.com/questions/2381560/how-to-group-a-3x3-grid-of-radio-buttons – Kewell Oct 17 '13 at 02:15

3 Answers3

1

I had the same problem, but as far as i know, it is not possible to do something like you are trying.

I searched a little bit and found a possible workarounds for this.

In another thread, i found out this explanation:

Your RadioButton widgets must be immediate children of the RadioGroup for the group effect to work. (Source here)

Another possible way, is to to something similar to the Table Layout Subclass solution and implement click listerners for each Radio Button and then control the checked status. (or you can create a manager class for this, it is not that hard, IMO).

Another way, in case you don't have many radio buttons, is to do something like this (it is not a pretty solution, but it works and i got that goin' for me, which is nice). In my example, i have to RadioButton, each one in a TableRow. I set the same Listener to both for CheckedChanged and when one of them gets checked, i uncheck the others (in this case, it is just one).

OnCheckedChangeListener listener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()) {
                case R.id.radiobutton_int_value:
                    if (isChecked) {
                        mRadioPrefStrValue.setChecked(false);
                    }
                    break;
                case R.id.radiobutton_str_value:
                    if (isChecked) {
                        mRadioPrefIntValue.setChecked(false);

                    }
                    break;
                default:
                    break;
            }
        }
    };

    mRadioPrefIntValue.setOnCheckedChangeListener(listener);
    mRadioPrefStrValue.setOnCheckedChangeListener(listener);

Or, if you have time to change your layout, use RelativeLayout. Unfortunately, i discovered this problem after my entire layout was done. It is not hard to change, but for what i need, the "workaround" above works and for this project, it is OK to go this way.

Community
  • 1
  • 1
Luis
  • 709
  • 8
  • 10
  • Cant thank you enough. I have been breaking my head since morning trying to achieve this. It works like a charm :) – ngrashia Jul 07 '14 at 11:27
0

Creating a RadioGroup Class and RadioButton Class, then applying to your xml would be easiest way IMO ..

See this source

Community
  • 1
  • 1
VipiN Negi
  • 2,994
  • 3
  • 24
  • 47
0

I answered a more general version of your same question here

The gist is to treat the radio buttons as regular buttons and handle the switching yourself. It's not as elegant as a well-designed radio button system should be, but I think is simpler than making a custom widget. We gotta work with what we got.

class FooActivity {

    RadioButton m_one, m_two, m_three;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        m_one = (RadioButton) findViewById(R.id.first_radio_button);
        m_two = (RadioButton) findViewById(R.id.second_radio_button);
        m_three = (RadioButton) findViewById(R.id.third_radio_button);

        m_one.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                m_one.setChecked(true);
                m_two.setChecked(false);
                m_three.setChecked(false);
            }
        });

        m_two.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                m_one.setChecked(false);
                m_two.setChecked(true);
                m_three.setChecked(false);
            }
        });

        m_three.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                m_one.setChecked(false);
                m_two.setChecked(false);
                m_three.setChecked(true);
            }
        });

        ...     
    } // onCreate() 
}
Variag
  • 1,056
  • 10
  • 25
SMBiggs
  • 11,034
  • 6
  • 68
  • 83