I have some RadioButtons and I want them to be checked/unchecked as the model changes, using Data Binding.
I managed to set up an EditText and it is working fine.
Anyway, the RadioButtons behave as the android:checked property wouldn't be there.
<RadioButton
android:id="@+id/radio_kitchen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kitchen"
android:checked="@{radiator.room==@string/kitchen?true:false}"
/>
You can create custom ids for each radio button and use android:checkedButton of radiogroup to retain checked state of radio button via two way data binding. For value of radio button you can use android:onClick attribute to have specific value according to your business logic
I managed to set up an EditText and it is working fine. Anyway, the RadioButtons behave as the android:checked property wouldn't be there.
The Data Binding Library is an Android Jetpack library that allows you to bind UI components in your XML layouts to data sources in your app using a declarative format rather than programmatically, reducing boilerplate code. This codelab has been designed for those with some Android development experience.
It is a regular layout with a ConstraintLayout as the root element. In order to convert the layout to Data Binding, you need to wrap the root element in a <layout> tag. You'll also have to move the namespace definitions (the attributes that start with xmlns:) to the new root element.
This is what I am currently doing for this:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> car.setModel(@string/car_model_toyota)}"
android:checked="@{car.model.equals(@string/car_model_toyota)}"
android:text="@string/car_model_toyota"/>
It's cheating since I am kind of giving some code logic to the View... but at least I don't have to implement an onchange listener on every single radiogroup and I still get the binding to happen...
Unless someone correct me and gives me a more ethic and professional solution I think I am going to stick with this one.
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