0

I am using this code here to split my radio buttons into rows and columns https://stackoverflow.com/a/2383978/3774526 but I am having a problem.

I can not detect when one of the radio buttons in the ToggleButtonGroupTableLayout is clicked.

This is what I do:

ToggleButtonGroupTableLayout group;
group = (ToggleButtonGroupTableLayout) findViewById(R.id.group);
group.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("check", "if clicked");
                }
            });

nothing inside the onClick function is executed. Any ideas? Thanks

zeeks
  • 775
  • 2
  • 12
  • 30
  • `I can not detect when one of the radio buttons in the ToggleButtonGroupTableLayout is clicked.` Because you set the listener on the group itself, not on its individual children. Then, you must click the group to fire the listener. – Phantômaxx Dec 03 '17 at 19:59
  • @NoiseGenerator if i set the listener on the individual children, then they all can be selected and I don't want that – zeeks Dec 03 '17 at 20:02
  • No, you have to set a listener for every child. **1:1**. – Phantômaxx Dec 03 '17 at 20:04
  • @NoiseGenerator I know, I have tried firstChild.setOnClickListener() also secondChild.setOnClickListener() and so on and this way if I touch on all the radio buttons, they all get selected, meaning, the previous do not get unselected – zeeks Dec 03 '17 at 20:06
  • This is because you didn't contain them inside a **RadioGroup**. This makes them mutually exclusive (you click one and the previously selected one is deselected). – Phantômaxx Dec 03 '17 at 20:07
  • @NoiseGenerator so I need both ToggleButtonGroupTableLayout and RadioGroup? – zeeks Dec 03 '17 at 20:09
  • I don't know what this `ToggleButtonGroupTableLayout` should be used for... try getting rid of it. The correct way is to use a `RadioGroup`. – Phantômaxx Dec 03 '17 at 20:10
  • @NoiseGenerator ToggleButtonGroupTableLayout is for displaying the RadioButtons in columns and rows. I just tested moving all the RadioButtons inside a RadioGroup and I still could choose all of them. – zeeks Dec 03 '17 at 20:13
  • There's something wrong in your layout arrangement. The RadioGroup has to be the **direct parent** of all the RadioButtons you want to make unique. RadioGroup inherits from LinearLayout and can be either vertical (default) or horizontal, not both. – Phantômaxx Dec 03 '17 at 20:15
  • @NoiseGenerator then how do I split the radio buttons in 2 columns? – zeeks Dec 03 '17 at 20:24
  • Possibly already answered. Just google for `android radiogroup grid` – Phantômaxx Dec 03 '17 at 21:27

1 Answers1

0

look on this code:

@Override
public void addView(View child, int index,
        android.view.ViewGroup.LayoutParams params) {
    super.addView(child, index, params);
    setChildrenOnClickListener((TableRow)child);
}


/* (non-Javadoc)
 * @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams)
 */
@Override
public void addView(View child, android.view.ViewGroup.LayoutParams params) {
    super.addView(child, params);
    setChildrenOnClickListener((TableRow)child);
}


private void setChildrenOnClickListener(TableRow tr) {
    final int c = tr.getChildCount();
    for (int i=0; i < c; i++) {
        final View v = tr.getChildAt(i);
        if ( v instanceof RadioButton ) {
            v.setOnClickListener(this);
        }
    }
}

they put a listener if the instance is RadioButton.

final View v = tr.getChildAt(i);
    if ( v instanceof RadioButton ) {
        v.setOnClickListener(this);
    }