I have so far the following code for that allows me to set a value to a textview when I click between toggle buttons that are grouped in a radiogroup. For some reason, it seems like the OnCheckedChanged does not get called. Is this the appproach for this?
//onClickListener method that returns an interface
private View.OnClickListener createClickListener(final int value) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
buttonValue = value;
buttonState = true;
ToggleButton clickedButton = (ToggleButton) view;
RadioGroup radioGroup= (RadioGroup) clickedButton.getParent();
final TextView num = (TextView) view.findViewById(R.id.num);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.number_zero:
buttonValue = value;
num.setText(buttonValue);
System.out.println("Button Value" + buttonValue);
case R.id.number_one:
buttonValue = value;
num.setText(buttonValue);
System.out.println("Button Value" + buttonValue);
case R.id.number_two:
buttonValue = value;
num.setText(buttonValue);
System.out.println("Button Value" + buttonValue);
case R.id.number_three:
buttonValue = value;
num.setText(buttonValue);
System.out.println("Button Value" + buttonValue);
}
System.out.println("Check changed listener called");
}
});
for (int i = 0; i < radioGroup.getChildCount(); ++i) {
View nextChild = radioGroup.getChildAt(i);
if (!(nextChild instanceof ToggleButton)) {
continue;
}
if (nextChild.getId() != clickedButton.getId() || !clickedButton.isChecked()) {
ToggleButton tb2 = (ToggleButton) nextChild;
tb2.setChecked(false);
}
}
}
};
}
Calling using:
zero.setOnClickListener(createClickListener(0));
one.setOnClickListener(createClickListener(1));
two.setOnClickListener(createClickListener(2));
three.setOnClickListener(createClickListener(3))
EDIT:
For some reason, the text is not being set to the TextView of num.
I changed above from
final TextView num = (TextView) view.findViewById(R.id.num);
num.setText(value);
to
num = (TextView) findViewById(R.id.num);
num.setText(Integer.toString(value));
making this:
TextView num;
a global variable.
I used the debugger and num is not null and the value is number of the button.