Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch case in Data Binding

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.

like image 436
Ravi Avatar asked Oct 25 '25 06:10

Ravi


2 Answers

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.

like image 110
BVantur Avatar answered Oct 26 '25 20:10

BVantur


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.

like image 32
Jan Slominski Avatar answered Oct 26 '25 18:10

Jan Slominski