View visibility dependent on CheckBox in data binding
I want to set a view visibility dependent on a CheckBox checked status. Something like we do in preference.xml.
Currently i am doing
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="isScheduleChecked"
type="java.lang.Boolean"/>
<variable
name="activity"
type="com.amelio.ui.activities.ActivityCart"/>
</data>
<LinearLayout
style="@style/llDefault"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onCheckedChanged="@{()-> isScheduleChecked}"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{isScheduleChecked ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
</LinearLayout>
</layout>
This does not work. I think android:onCheckedChanged="@{()-> isScheduleChecked}"
this line is not working. What i am doing wrong? Some tell me best way to implement it.
Currently i am changing isScheduleChecked by my activity in java code like binding.setIsScheduleChecked(true/false);
but i don't to write code in java class for just set visibility.
android android-layout checkbox android-databinding
add a comment |
I want to set a view visibility dependent on a CheckBox checked status. Something like we do in preference.xml.
Currently i am doing
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="isScheduleChecked"
type="java.lang.Boolean"/>
<variable
name="activity"
type="com.amelio.ui.activities.ActivityCart"/>
</data>
<LinearLayout
style="@style/llDefault"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onCheckedChanged="@{()-> isScheduleChecked}"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{isScheduleChecked ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
</LinearLayout>
</layout>
This does not work. I think android:onCheckedChanged="@{()-> isScheduleChecked}"
this line is not working. What i am doing wrong? Some tell me best way to implement it.
Currently i am changing isScheduleChecked by my activity in java code like binding.setIsScheduleChecked(true/false);
but i don't to write code in java class for just set visibility.
android android-layout checkbox android-databinding
add a comment |
I want to set a view visibility dependent on a CheckBox checked status. Something like we do in preference.xml.
Currently i am doing
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="isScheduleChecked"
type="java.lang.Boolean"/>
<variable
name="activity"
type="com.amelio.ui.activities.ActivityCart"/>
</data>
<LinearLayout
style="@style/llDefault"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onCheckedChanged="@{()-> isScheduleChecked}"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{isScheduleChecked ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
</LinearLayout>
</layout>
This does not work. I think android:onCheckedChanged="@{()-> isScheduleChecked}"
this line is not working. What i am doing wrong? Some tell me best way to implement it.
Currently i am changing isScheduleChecked by my activity in java code like binding.setIsScheduleChecked(true/false);
but i don't to write code in java class for just set visibility.
android android-layout checkbox android-databinding
I want to set a view visibility dependent on a CheckBox checked status. Something like we do in preference.xml.
Currently i am doing
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="isScheduleChecked"
type="java.lang.Boolean"/>
<variable
name="activity"
type="com.amelio.ui.activities.ActivityCart"/>
</data>
<LinearLayout
style="@style/llDefault"
android:layout_height="match_parent"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onCheckedChanged="@{()-> isScheduleChecked}"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{isScheduleChecked ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
</LinearLayout>
</layout>
This does not work. I think android:onCheckedChanged="@{()-> isScheduleChecked}"
this line is not working. What i am doing wrong? Some tell me best way to implement it.
Currently i am changing isScheduleChecked by my activity in java code like binding.setIsScheduleChecked(true/false);
but i don't to write code in java class for just set visibility.
android android-layout checkbox android-databinding
android android-layout checkbox android-databinding
asked Apr 28 '18 at 5:20
Khemraj
11.9k23170
11.9k23170
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
That's a cool idea! I got it to work by replacing your onCheckedChanged
line with:
android:checked="@={isScheduleChecked}"
I'll check it :), can you tell me what this lamda expression do exactly.
– Khemraj
Apr 29 '18 at 16:36
2
The "=" between @ and { makes it two-way binding. This means that when you change the value on the view, it changes the variable it is bound to (one way binding would only update the view based on variable change). You can use that to get input text from a user, for example. Thechecked
attribute reflects the current state of the CheckBox and will update to true/false based on user checking/unchecking. I tried using the event similar to how you did, but I could only find how to call methods, not assignment expressions. Let me know if it works for you.
– Alan Todtenkopf
Apr 29 '18 at 17:23
I would able to test it tomorrow at my office, will mark right for sure if it works. by the way great explanation. I appreciate your effort.
– Khemraj
Apr 29 '18 at 17:32
So i was doing one way binding.
– Khemraj
Apr 29 '18 at 17:33
Thank you so much @Alan, It worked well and i removed extra 10 lines of xml and code that i wrote to make it work.
– Khemraj
Apr 30 '18 at 5:21
add a comment |
I did not know the Easiest Way before.
You can refer to id in data binding. No need to take another variable.
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
Reasons that may cause issue
- Ids are always generated in
camelCase
. like id ischeck_box
then you will usecheckBox.isChecked()
.
You must import
View
in layout to use itView.VISIBLE
<data>
<import type="android.view.View"/>
</data>
If you are having any other issue then you can comment.
any idea why this one isn't working for me (Alan Todtenkopf answer is)?
– svkaka
Nov 12 '18 at 20:29
See reasons, I edited answer. Can you also post your error in comment?
– Khemraj
Nov 13 '18 at 4:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50073150%2fview-visibility-dependent-on-checkbox-in-data-binding%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
That's a cool idea! I got it to work by replacing your onCheckedChanged
line with:
android:checked="@={isScheduleChecked}"
I'll check it :), can you tell me what this lamda expression do exactly.
– Khemraj
Apr 29 '18 at 16:36
2
The "=" between @ and { makes it two-way binding. This means that when you change the value on the view, it changes the variable it is bound to (one way binding would only update the view based on variable change). You can use that to get input text from a user, for example. Thechecked
attribute reflects the current state of the CheckBox and will update to true/false based on user checking/unchecking. I tried using the event similar to how you did, but I could only find how to call methods, not assignment expressions. Let me know if it works for you.
– Alan Todtenkopf
Apr 29 '18 at 17:23
I would able to test it tomorrow at my office, will mark right for sure if it works. by the way great explanation. I appreciate your effort.
– Khemraj
Apr 29 '18 at 17:32
So i was doing one way binding.
– Khemraj
Apr 29 '18 at 17:33
Thank you so much @Alan, It worked well and i removed extra 10 lines of xml and code that i wrote to make it work.
– Khemraj
Apr 30 '18 at 5:21
add a comment |
That's a cool idea! I got it to work by replacing your onCheckedChanged
line with:
android:checked="@={isScheduleChecked}"
I'll check it :), can you tell me what this lamda expression do exactly.
– Khemraj
Apr 29 '18 at 16:36
2
The "=" between @ and { makes it two-way binding. This means that when you change the value on the view, it changes the variable it is bound to (one way binding would only update the view based on variable change). You can use that to get input text from a user, for example. Thechecked
attribute reflects the current state of the CheckBox and will update to true/false based on user checking/unchecking. I tried using the event similar to how you did, but I could only find how to call methods, not assignment expressions. Let me know if it works for you.
– Alan Todtenkopf
Apr 29 '18 at 17:23
I would able to test it tomorrow at my office, will mark right for sure if it works. by the way great explanation. I appreciate your effort.
– Khemraj
Apr 29 '18 at 17:32
So i was doing one way binding.
– Khemraj
Apr 29 '18 at 17:33
Thank you so much @Alan, It worked well and i removed extra 10 lines of xml and code that i wrote to make it work.
– Khemraj
Apr 30 '18 at 5:21
add a comment |
That's a cool idea! I got it to work by replacing your onCheckedChanged
line with:
android:checked="@={isScheduleChecked}"
That's a cool idea! I got it to work by replacing your onCheckedChanged
line with:
android:checked="@={isScheduleChecked}"
answered Apr 29 '18 at 14:30
Alan Todtenkopf
24725
24725
I'll check it :), can you tell me what this lamda expression do exactly.
– Khemraj
Apr 29 '18 at 16:36
2
The "=" between @ and { makes it two-way binding. This means that when you change the value on the view, it changes the variable it is bound to (one way binding would only update the view based on variable change). You can use that to get input text from a user, for example. Thechecked
attribute reflects the current state of the CheckBox and will update to true/false based on user checking/unchecking. I tried using the event similar to how you did, but I could only find how to call methods, not assignment expressions. Let me know if it works for you.
– Alan Todtenkopf
Apr 29 '18 at 17:23
I would able to test it tomorrow at my office, will mark right for sure if it works. by the way great explanation. I appreciate your effort.
– Khemraj
Apr 29 '18 at 17:32
So i was doing one way binding.
– Khemraj
Apr 29 '18 at 17:33
Thank you so much @Alan, It worked well and i removed extra 10 lines of xml and code that i wrote to make it work.
– Khemraj
Apr 30 '18 at 5:21
add a comment |
I'll check it :), can you tell me what this lamda expression do exactly.
– Khemraj
Apr 29 '18 at 16:36
2
The "=" between @ and { makes it two-way binding. This means that when you change the value on the view, it changes the variable it is bound to (one way binding would only update the view based on variable change). You can use that to get input text from a user, for example. Thechecked
attribute reflects the current state of the CheckBox and will update to true/false based on user checking/unchecking. I tried using the event similar to how you did, but I could only find how to call methods, not assignment expressions. Let me know if it works for you.
– Alan Todtenkopf
Apr 29 '18 at 17:23
I would able to test it tomorrow at my office, will mark right for sure if it works. by the way great explanation. I appreciate your effort.
– Khemraj
Apr 29 '18 at 17:32
So i was doing one way binding.
– Khemraj
Apr 29 '18 at 17:33
Thank you so much @Alan, It worked well and i removed extra 10 lines of xml and code that i wrote to make it work.
– Khemraj
Apr 30 '18 at 5:21
I'll check it :), can you tell me what this lamda expression do exactly.
– Khemraj
Apr 29 '18 at 16:36
I'll check it :), can you tell me what this lamda expression do exactly.
– Khemraj
Apr 29 '18 at 16:36
2
2
The "=" between @ and { makes it two-way binding. This means that when you change the value on the view, it changes the variable it is bound to (one way binding would only update the view based on variable change). You can use that to get input text from a user, for example. The
checked
attribute reflects the current state of the CheckBox and will update to true/false based on user checking/unchecking. I tried using the event similar to how you did, but I could only find how to call methods, not assignment expressions. Let me know if it works for you.– Alan Todtenkopf
Apr 29 '18 at 17:23
The "=" between @ and { makes it two-way binding. This means that when you change the value on the view, it changes the variable it is bound to (one way binding would only update the view based on variable change). You can use that to get input text from a user, for example. The
checked
attribute reflects the current state of the CheckBox and will update to true/false based on user checking/unchecking. I tried using the event similar to how you did, but I could only find how to call methods, not assignment expressions. Let me know if it works for you.– Alan Todtenkopf
Apr 29 '18 at 17:23
I would able to test it tomorrow at my office, will mark right for sure if it works. by the way great explanation. I appreciate your effort.
– Khemraj
Apr 29 '18 at 17:32
I would able to test it tomorrow at my office, will mark right for sure if it works. by the way great explanation. I appreciate your effort.
– Khemraj
Apr 29 '18 at 17:32
So i was doing one way binding.
– Khemraj
Apr 29 '18 at 17:33
So i was doing one way binding.
– Khemraj
Apr 29 '18 at 17:33
Thank you so much @Alan, It worked well and i removed extra 10 lines of xml and code that i wrote to make it work.
– Khemraj
Apr 30 '18 at 5:21
Thank you so much @Alan, It worked well and i removed extra 10 lines of xml and code that i wrote to make it work.
– Khemraj
Apr 30 '18 at 5:21
add a comment |
I did not know the Easiest Way before.
You can refer to id in data binding. No need to take another variable.
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
Reasons that may cause issue
- Ids are always generated in
camelCase
. like id ischeck_box
then you will usecheckBox.isChecked()
.
You must import
View
in layout to use itView.VISIBLE
<data>
<import type="android.view.View"/>
</data>
If you are having any other issue then you can comment.
any idea why this one isn't working for me (Alan Todtenkopf answer is)?
– svkaka
Nov 12 '18 at 20:29
See reasons, I edited answer. Can you also post your error in comment?
– Khemraj
Nov 13 '18 at 4:58
add a comment |
I did not know the Easiest Way before.
You can refer to id in data binding. No need to take another variable.
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
Reasons that may cause issue
- Ids are always generated in
camelCase
. like id ischeck_box
then you will usecheckBox.isChecked()
.
You must import
View
in layout to use itView.VISIBLE
<data>
<import type="android.view.View"/>
</data>
If you are having any other issue then you can comment.
any idea why this one isn't working for me (Alan Todtenkopf answer is)?
– svkaka
Nov 12 '18 at 20:29
See reasons, I edited answer. Can you also post your error in comment?
– Khemraj
Nov 13 '18 at 4:58
add a comment |
I did not know the Easiest Way before.
You can refer to id in data binding. No need to take another variable.
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
Reasons that may cause issue
- Ids are always generated in
camelCase
. like id ischeck_box
then you will usecheckBox.isChecked()
.
You must import
View
in layout to use itView.VISIBLE
<data>
<import type="android.view.View"/>
</data>
If you are having any other issue then you can comment.
I did not know the Easiest Way before.
You can refer to id in data binding. No need to take another variable.
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkbox"/>
<LinearLayout
style="@style/llDefault"
android:padding="@dimen/space_small"
android:visibility="@{checkbox.isChecked() ? View.VISIBLE : View.GONE, default = gone}"
>
</LinearLayout>
Reasons that may cause issue
- Ids are always generated in
camelCase
. like id ischeck_box
then you will usecheckBox.isChecked()
.
You must import
View
in layout to use itView.VISIBLE
<data>
<import type="android.view.View"/>
</data>
If you are having any other issue then you can comment.
edited Nov 13 '18 at 4:56
answered Oct 28 '18 at 5:02
Khemraj
11.9k23170
11.9k23170
any idea why this one isn't working for me (Alan Todtenkopf answer is)?
– svkaka
Nov 12 '18 at 20:29
See reasons, I edited answer. Can you also post your error in comment?
– Khemraj
Nov 13 '18 at 4:58
add a comment |
any idea why this one isn't working for me (Alan Todtenkopf answer is)?
– svkaka
Nov 12 '18 at 20:29
See reasons, I edited answer. Can you also post your error in comment?
– Khemraj
Nov 13 '18 at 4:58
any idea why this one isn't working for me (Alan Todtenkopf answer is)?
– svkaka
Nov 12 '18 at 20:29
any idea why this one isn't working for me (Alan Todtenkopf answer is)?
– svkaka
Nov 12 '18 at 20:29
See reasons, I edited answer. Can you also post your error in comment?
– Khemraj
Nov 13 '18 at 4:58
See reasons, I edited answer. Can you also post your error in comment?
– Khemraj
Nov 13 '18 at 4:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50073150%2fview-visibility-dependent-on-checkbox-in-data-binding%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown