Develop your application faster & smarter with Data Binding — Expressions — Part 3

Harin Trivedi
AndroidPub
Published in
3 min readMay 2, 2017

Hello everyone👋🏼, you might have looked into Part 2 of data binding and got some idea about event binding. Now you have got data in your layout, but you might want to operate on data or want to represent them according to specific requirements.

Generally the data objects are made by consuming some sort of web services where it manages in some pre-defined manner. But the presentation will always be different, where sometimes you need to combine some values or even operate on values to show output of it!

So expressions come to rescue here..! Yes, using expressions in data binding will allow you to represent your data, your own way. It’s very easy and similar like when you manage to set any value to any component, from the code, now you can write it directly inside the layout!

You can use all these expressions in layout to operate on data on the go…!!

  • Mathematical + - / * %
  • String concatenation +
  • Logical && ||
  • Binary & | ^
  • Unary + - ! ~
  • Shift >> >>> <<
  • Comparison == > < >= <=
  • instanceof
  • Grouping ()
  • Literals — character, String, numeric, null
  • Cast
  • Method calls
  • Field access
  • Array access []
  • Ternary operator? :
ref:https://giphy.com

Yes, you might be feeling same!! Let’s see how to use it.

  • Using Expressions
//To display first and last name with concatenation
android:text="@{user.firstName + ' ' + user.lastName}
//To concate string value with variable
android:text='@{"Welcome, " + user.firstName}'
//To use Ternary operator for setting visibility
android:visibility="@{user.age < 18 ? View.GONE : View.VISIBLE}"
//To access value from array
android:text="@{user.followersList[index]}"
//To access value from map
android:text="@{user.settingsMap[key]}"
  • Null coalescing operator (??) chooses the left operand if it is not null or the right if it is null.
android:text="@{user.displayName ?? user.firstName}"//Equivalent to belowandroid:text="@{user.displayName != null ? user.displayName : user.firstName}"
  • You can call any method from instance of a class directly inside the layout!
//To convert into string with method call
android:text="@{String.valueOf(user.age)}"
//To calculate age from static method and display it
<import type="com.example.Util"/>
...
android:text="@{String.valueOf(Util.calculateAge(user.dob))}"
//To call method of data model to convert date to string
android:text="@{data.convertToString(data.dob)}"
  • Ok, Great! Now let’s see examples to use values from references within expressions.
//To use 
android:text="@{@string/val_hi}"
android:text="@{@string/val_hi + @string/msg_welcome}"android:layout_height="@{@dimen/img_height"}"android:layout_height="@{data.isThumbnail ? @dimen/img_tumb_height : @dimen/img_full_height}"android:background="@{data.isReadMsg ? @color/dray : @color/white}"
  • You might also be using dynamic string values by appending value in between.
//In string.xml
<string name="str_your_follower_count">You have %1$d followers</string>
//In layout
android:text="@{String.format(@string/msg_your_follower_count, data.followerCount)}"

These are only few exmaples how you can present your data with verity of expressions. There may be many other combinations as per your requirements and your imagination!

--

--

Harin Trivedi
AndroidPub

Software Engineer, Exploring the world of Technology!