1

In my layout I have a checkbox, which should toggle the enabled state of a different view. This means, the checked value of one view, should be bound to the enabled value of a different view.

To me this sounds like a perfect use case for the new data binding library, which should be a simple line in xml in my opinion, without having to use Java code. Something like android:enabled="@{(@id/chk_enabled).isChecked()}" I'm thinking. Sadly enough this isn't valid, and I can't find any information on how to do this correctly.

That's why I'm wondering, is this even possible?

2
  • @PN10: Your solution requires me to bind a variable called state to my UI, which requires java to do something purely UI related. That's something I wanted to avoid. The answer from szymon2013 is what I was looking for Commented Oct 13, 2016 at 12:08
  • yeah ...good to know you got solution .. Commented Oct 13, 2016 at 13:18

1 Answer 1

2

try this:

<CheckBox
            android:id="@+id/showMore"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

            <EditText
                android:enabled="@{showMore.checked ?true:false}"
                android:layout_width="match_parent"
                android:layout_height="20dp" />
Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks, this worked. You can even shorten it by dropping the true/false like this: android:enabled="@{showMore.checked}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.