Enable view when RadioButton checked

58 views Asked by At

I have a View (i.e. a TextInputLayout, Button, whatever) that is disabled but once the RadioButton is checked I want to enable that view. And, once the radioButton is unchecked, I want that view to be disabled. I want to do it using DataBinding, but it is not working... Help?

        <RadioGroup
            android:id="@+id/radio_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/myself"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/myself"/>

            <RadioButton
                android:id="@+id/group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/group" />
        </RadioGroup>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/groups_layout"
            style="@style/MyAutoCompleteTextView.Layout.Dense"
            android:enabled="@{group.isChecked()}"/>
1

There are 1 answers

0
Kamal Nayan On

You need to set the listener of the radio button in your code and accordingly update a liveData, then here in the layout just use androd:enabled=@{VIEW_MODEL.LIVE_DATA}

NOTE : Make sure that you import the viewModel in the respective layout.