Search code examples
androidandroid-databindingandroid-radiogroup

How to bind method on RadioGroup on checkChanged event with data-binding


I was searching over the internet for how to perform the new cool android data-binding over the RadioGroup and I didn't find a single blog post about it.

Its a simple scenario, based on the radio button selected, I want to attach a callback event using android data binding. I don't find any method on the xml part which allows me to define a callback.

Like here is my RadioGroup:

      <RadioGroup
            android:id="@+id/split_type_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:checkedButton="@+id/split_type_equal"
            android:gravity="center"
            <!-- which tag ? -->
            android:orientation="horizontal">

           ...

       </RadioGroup>

How do I attach a handler method which will be called on RadioGroup's checkChnged event will fire using data-binding?

I have tried using onClick (don't know if it is the same) in layout file and defining method in the Activity and located it using this in the layout file:

   <variable
        name="handler"
        type="com.example.MainActivity"/>

  ...
   <RadioGroup
        android:onClick="handler.onCustomCheckChanged"
        .. >

And defined method onCustomCheckChanged like this:

public void onCustomCheckChanged(RadioGroup radio, int id) {
     // ...
}

But, it gives me the compilation error:

Error:(58, 36) Listener class android.view.View.OnClickListener with method onClick did not match signature of any method handler.onCustomCheckChanged

I have seen many blogs mentioning it is possible with RadioGroup but non of them really say how. How can I handle this with data-binding ?


Solution

  • After digging to the bunch of methods, I found this question on SO which helped me understand how to bind single methods of listeners.

    Here is what to do with RadioGroup:

    In RadioGroup listener you have a method onCheckedChanged(RadioGroup g, int id). So you can directly bound that method to your handler or your activity by passing an instance of it as a variable in layout file and calling a method with the same signature.

    So call on layout file like this:

      <RadioGroup
            android:id="@+id/split_type_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:checkedButton="@+id/split_type_equal"
            android:gravity="center"
            android:onCheckedChanged="@{handler.onSplitTypeChanged}"
            android:orientation="horizontal">
    
           ...
    
       </RadioGroup>
    

    And in my activity or handler, I need to simply provide the method with same name and signature like this:

    public void onSplitTypeChanged(RadioGroup radioGroup,int id) {
      // ...
    }
    

    Just make sure method is public.

    NOTE: This works for any (most of, I have not tried all) listener methods. Like for EditText you can provide android:onTextChanged and so on.