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
?
The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time. // Avoids infinite loops.
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.
I am using a string, and in this case I have bindable based on viewModel.getCommuteType() viewModel.setCommuteType(String)
<RadioGroup android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:checked="@{viewModel.commuteType.equals(Commute.DRIVING)}" android:onClick="@{()->viewModel.setCommuteType(Commute.DRIVING)}" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="D"/> <RadioButton android:checked="@{viewModel.commuteType.equals(Commute.BICYCLE)}" android:onClick="@{()->viewModel.setCommuteType(Commute.BICYCLE)}" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B"/> <RadioButton android:checked="@{viewModel.commuteType.equals(Commute.WALKING)}" android:onClick="@{()->viewModel.setCommuteType(Commute.WALKING)}" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="W"/> <RadioButton android:checked="@{viewModel.commuteType.equals(Commute.BUS)}" android:onClick="@{()->viewModel.setCommuteType(Commute.BUS)}" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="T"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With