Is it possible to write switch case with android Data Binding?
Suppose i have 3 conditions like
value == 1 then print A
value == 2 then print B
value == 3 then print C
Does there any way to do this stuff in xml by using Data Binding?
I know we can implement conditional statement like
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
But here i am searching for switch case statement.
This is definitely better to do in business logic in seperate java class, but if you want to do this with databinding inside xml file, than you have to do it with more inline if statements like this:
android:text='@{TextUtils.equals(value, "1") ? "A" : TextUtils.equals(value, "2") ? "B" : TextUtils.equals(value, "3") ? "C" : ""}'
As you see you have to add every next condition in else state, which makes everything terrible to read.
No, as far as I know it is not possible and also would make the xml files really unreadable. I think it would be better to implement this in your business logic, not in layout files.
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