Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group RadioButton from different LinearLayouts?

I was wondering if is possible to group each single RadioButton in a unique RadioGroup maintaining the same structure. My structure look like this:

  • LinearLayout_main
    • LinearLayout_1
      • RadioButton1
    • LinearLayout_2
      • RadioButton2
    • LinearLayout_3
      • RadioButton3

As you can see, now each RadioButton is a child of different LinearLayout. I tried using the structure below, but it doesn't work:

  • Radiogroup
    • LinearLayout_main
      • LinearLayout_1
        • RadioButton1
      • LinearLayout_2
        • RadioButton2
      • LinearLayout_3
        • RadioButton3
like image 378
marcoqf73 Avatar asked May 05 '12 10:05

marcoqf73


People also ask

How do I group radio buttons in VB net?

To group RadioButton controls as a set to function independently of other sets. Drag a GroupBox or Panel control from the Windows Forms tab on the Toolbox onto the form. Draw RadioButton controls on the GroupBox or Panel control.

What is used to group together one or more radio buttons in Android?

To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup .

How do I add radio buttons to my radio group?

List<String> list = new ArrayList<String>(); list. add("one"); list. add("two"); list. add("three"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.

How do you change a radio group from horizontal to vertical?

Yes, there is a way. Drag a radio list widget to your screen, go to the Properties tab and select 'Orientation' -> Horizontal.


Video Answer


1 Answers

It seems that the good people at Google/Android assume that when you use RadioButtons, you don't need the flexibility that comes with every other aspect of the Android UI/layout system. To put it simply: they don't want you to nest layouts and radio buttons. Sigh.

So you gotta work around the problem. That means you must implement radio buttons on your own.

This really isn't too hard. In your onCreate(), set your RadioButtons with their own onClick() so that when they are activated, they setChecked(true) and do the opposite for the other buttons. For example:

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()   } 

Yeah, I know--way old-school. But it works. Good luck!

like image 155
SMBiggs Avatar answered Sep 17 '22 14:09

SMBiggs