0

I've an alert dialog, which has custom view containing two Radio buttons in a RadioGroup. Now when I set OnCheckChangedListener on this RadioGroup by inflating the custom view, It is not triggered. Also no error log is shown.

I've followed this post before asking here: Link

Following is my code for alert dialog.

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this, R.style.AlertDialogStyle);
    alt_bld.setTitle("Select Date");
    alt_bld.setView(R.layout.alert_dialog_radiogroup);

    //here I'm setting listener. 
    try{
        final View view = View.inflate(addAlarm.this, R.layout.alert_dialog_radiogroup, null);
        radioGroup = (RadioGroup)view.findViewById(R.id.radio);
        radioGroup.clearCheck();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                //My code to handle checked radio button. 
            }
        });
    } catch (Exception e){
        e.printStackTrace();
    }

    //setting +ve and -ve buttons here.
    ...

    AlertDialog alert = alt_bld.create();
    alert.show();

And this is my custom view for alert dialog.

alert_dialog_radiogroup.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100sp" >

<RadioGroup
    android:layout_width="match_parent"
    android:id="@+id/radio"
    android:layout_marginTop="10sp"
    android:layout_marginStart="25sp"
    android:layout_marginBottom="15sp"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
        android:id="@+id/track_everyday"
        android:textColor="@color/colorPrimaryText"
        android:layout_width="wrap_content"
        android:buttonTint="@color/colorAccent"
        android:layout_height="wrap_content"
        android:text="@string/everyday" />

    <RadioButton
        android:id="@+id/custom_track"
        android:layout_marginTop="8sp"
        android:textColor="@color/colorPrimaryText"
        android:layout_width="wrap_content"
        android:buttonTint="@color/colorAccent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rd"
        android:text="@string/custom" />

</RadioGroup>

I've searched this issue before asking here. In some posts, answer is to clear the check from all the radio buttons before setting the listener. I've tried this by radioGroup.clearCheck(); , but not working.

So what am I missing here? Thank you for the help.

Kaushal28
  • 5,377
  • 5
  • 41
  • 72

1 Answers1

0

You are setting the view before initialising the listeners on it.

Try this :

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this, R.style.AlertDialogStyle);
alt_bld.setTitle("Select Date");


try{
    final View view = View.inflate(addAlarm.this, R.layout.alert_dialog_radiogroup, null);
    radioGroup = (RadioGroup)view.findViewById(R.id.radio);
    radioGroup.clearCheck();
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            //My code to handle checked radio button. 
        }

    });
    alt_bld.setView(view);
} catch (Exception e){
    e.printStackTrace();    
}

//setting +ve and -ve buttons here.
...

AlertDialog alert = alt_bld.create();
alert.show();

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23