0

I have created a custom three-button radio group programmatically. However, when I use the object in my fragment and set an onCheckChangedListener, it appears to not be responding.

Here is the code for my radioGroup:

public class ThreeButtonRadioGroup extends RadioGroup {

    View v;
    Context mContext;
    LayoutInflater mInflater;

    int dim = ((2* DimensionsController.getInstance().getScreenWidth()) / 8)-5;


    public ThreeButtonRadioGroup(Context context) {
        super(context);
        mInflater = LayoutInflater.from(mContext);
        init();
    }

    public ThreeButtonRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    RoofUIRadioButton button1, button2, button3;

    public void init(){
        mInflater = LayoutInflater.from(mContext);
        v = mInflater.inflate(R.layout.roof_ui_macro_frequency, this, true);

        (button1 = (RoofUIRadioButton) v.findViewById(R.id.button1)).initLayout(dim);
        (button2 = (RoofUIRadioButton) v.findViewById(R.id.button2)).initLayout(dim);
        (button3 = (RoofUIRadioButton) v.findViewById(R.id.button3)).initLayout(dim);

    }

    public void setButtonText(String[] buttonText, int preClick) {
        button1.setText(buttonText[0]);
        button2.setText(buttonText[1]);
        button3.setText(buttonText[2]);

        switch(preClick){
            case 1:
                button1.setChecked(true);
                break;
            case 2:
                button2.setChecked(true);
                break;
            case 3:
                button3.setChecked(true);
                break;
        }
    }

    public void setButtonText(String[] buttonText) {
        button1.setText(buttonText[0]);
        button2.setText(buttonText[1]);
        button3.setText(buttonText[2]);
    }
}

Here is the xml which I am inflating for the radio group:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:dividerPadding="20dp"
android:showDividers="middle">

<RadioButton
    android:id="@+id/button1"
    android:button="@null"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:padding="15dp"
    android:textSize="30sp"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_vertical|center_horizontal"
    style="@style/circleButton_style"
    android:tag="1"
    />

<RadioButton
    android:id="@+id/button2"
    android:button="@null"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:padding="15dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:textSize="30sp"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_vertical|center_horizontal"
    style="@style/circleButton_style"
    android:tag="2"
    />

<RadioButton
    android:id="@+id/button3"
    android:button="@null"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:padding="15dp"
    android:textSize="30sp"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_vertical|center_horizontal"
    style="@style/circleButton_style"
    android:tag="3"
    />

And here is where I am trying to apply the event listener:

        microRadioGroup = (ThreeButtonRadioGroup) view.findViewById(R.id.micro_radio_group);
    microRadioGroup.setButtonText(new String[]{"Day","Week","Month"}, 3);
    microRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
                System.out.println("CHECKED BUTTON: ");

        }
    });
ekad
  • 14,436
  • 26
  • 44
  • 46
  • In the second code sample, you're looking for a view with id `micro_radio_group` which I don't see in the XML you've provided. Are you sure things are wired up the way you expect? – Doug Stevenson Feb 13 '16 at 19:45

1 Answers1

0

Try like this.

RadioGroup group=(RadioGroup) findViewById(R.id.radioGroup1);

    group.setOnCheckedChangeListener(new OnCheckedChangeListener() 
       {

        public void onCheckedChanged(RadioGroup group, int checkedId) 
           {
            // TODO Auto-generated method stub
            if(radiobutton1.isChecked())
              {
                   System.out.println("CHECKED BUTTON: ");
              }
            else if(radiobutton2.isChecked())
              {


              }
         }
    });

Also try with using checked id.

switch(checkedId) {
    case R.id.radioButton1:
        break;
    case R.id.radioButton2:
        break;
    case R.id.radioButton3:
        break;
    }
}

Hope this will help you!!

Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • It's still not working at all. It seems the listener isn't getting called no matter what. I.e., I tried issuing a print statement whenever any event occurs in the listener, and it's giving me nothing. Are my buttons not attached to the listener in some way? –  Feb 13 '16 at 19:07
  • check my edited answer and try also debug your code. – Jay Rathod Feb 13 '16 at 19:14