Android data binding, Radio Button not updating
Asked Answered
E

4

7

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}"
/>
Extramural answered 15/2, 2016 at 18:48 Comment(5)
You probably intend .equals instead of ==Kaylenekayley
I also tried .equals, anyway i'm not sure it is supportedExtramural
This should work: "@{radiator.room.equals(@string/kitchen)}". If not, please file a bug because it should work.Kaylenekayley
is radiator.room returning string resource id or string?Backman
@GeorgeMount Perfect answer! It took me an hour to analyze this..Aeolian
D
4

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.

Dromedary answered 21/1, 2017 at 4:40 Comment(0)
P
0

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

<RadioGroup
    android:checkedButton="@={signUpModel.genderId}"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@id/headerGender"
    android:orientation="horizontal"
    android:layout_width="wrap_content" android:layout_height="wrap_content">                

    <RadioButton
        android:id="@+id/female"
        android:onClick="@{()->signUpModel.setGender(@string/cd_female)}"
        android:background="@drawable/bg_gender_cta_selector"
        android:button="@drawable/ic_gender_female"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:contentDescription="@string/cd_female"/>

    <RadioButton
        android:id="@+id/male"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:onClick="@{()->signUpModel.setGender(@string/cd_male)}"
        android:button="@drawable/ic_gender_male"
        android:background="@drawable/bg_gender_cta_selector"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:contentDescription="@string/cd_male"/>
</RadioGroup>

ids.xml(Inside values)

<resources>
    <item name="female" type="id" format="string"/>
    <item name="male" type="id" format="string"/>
</resources>
Pluralize answered 18/9, 2019 at 18:6 Comment(0)
R
0
 <RadioGroup ...>     
 <com.google.android.material.radiobutton.MaterialRadioButton
                                android:id="@+id/male"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="male"
                                android:value="male"
                                android:onClick="@{()->user.setGender(@string/male)}"
                                android:textColor="@color/colorPrimary" />

On string item set this works on my end

<string name="female">female</string>
<string name="male">male</string>
Reader answered 16/9, 2021 at 18:4 Comment(0)
M
-1

This answer might be late but others those who are facing this issue can try out:

XML:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Iron Man"
        android:onClick="buttonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Batman"
        android:onClick="buttonClicked"/>
</RadioGroup>

Activity:

public void buttonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.iron_man:
            if (checked)
                // Iron man is the best!
            break;
        case R.id.batman:
            if (checked)
                // Batman rocks!
            break;
    }
}

This is the method that I found on Android Developers page.

Metaphosphate answered 14/3, 2019 at 9:55 Comment(1)
you haven't even used databinding in your answer.Dacia

© 2022 - 2024 — McMap. All rights reserved.