Erik
Erik

Reputation: 21

Custom Radio-Group, add "setOnCheckedChangeListener"

Okay, first of all I have a custom radiogroup because i want different radiogroup layout e.g. 3x3 radiogroup.

The class looks like this:

public class CustomRadioButtons extends TableLayout implements OnClickListener{

private static final String TAG = "ToggleButtonGroupTableLayout";
private RadioButton activeRadioButton;

public CustomRadioButtons(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public CustomRadioButtons(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}


@Override
public void onClick(View v) {
    final RadioButton rb = (RadioButton) v;
    if ( activeRadioButton != null ) {
        activeRadioButton.setChecked(false);
    }
    rb.setChecked(true);
    activeRadioButton = rb;
}

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

@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);
        }
    }
}

public int getCheckedRadioButtonId() {
    if ( activeRadioButton != null ) {
        return activeRadioButton.getId();
    }
    return -1;
}

public void resetGroup() {
    if ( activeRadioButton != null ) {
        activeRadioButton.setChecked(false);
    }
}

public String getCheckedRadioButtonText() {
    if ( activeRadioButton != null ) {
        return activeRadioButton.getText().toString();
    }
    return "";
}

}

In my mainactivity I try want to use the CustomRadioButton from an inner class (or from an alertdialog to be more precise) as follows:

    final LayoutInflater[] inflater = {(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)};
    final View vi = inflater[0].inflate(R.layout.alertdialogxml, null);
    final CustomRadioButtons radioGroup = (CustomRadioButtons) vi.findViewById(R.id.radioGroup);

//this is how i would proceed (and it works) if this was a regular radioGroup.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //I wanna do Stuff Here.
            }
        });

So my question is, how do i set the OnCheckedChangeListner in this case. I guess i have to modify the "CustomRadioButtons" class, but i don't know how.

Thanks for help! =)

Upvotes: 0

Views: 287

Answers (0)

Related Questions