Skip to content

Instantly share code, notes, and snippets.

@faruktoptas
Last active August 2, 2019 15:01
Show Gist options
  • Save faruktoptas/47d97065342a40a88699df4b78f0428a to your computer and use it in GitHub Desktop.
Save faruktoptas/47d97065342a40a88699df4b78f0428a to your computer and use it in GitHub Desktop.
DataBinding samples
// Conditional string
android:text="@{viewModel.startButtonText ? @string/title_start_travel : @string/title_start}"
// Conditional visibility
<import type="android.view.View" />
android:visibility="@{viewModel.visibility == 1 ? View.VISIBLE : View.GONE}"
// Non-string text
android:text="@{String.valueOf(data.minutes)}"
// Binding Adapter
bind:profileUrl="@{user.imageUrl()}"
@BindingAdapter("bind:profileUrl")
fun loadImageProfile(view: ImageView, imageUrl: String) {
Picasso.get()
.load(imageUrl)
.transform(CircleTransform())
.placeholder(R.drawable.profile_placeholder)
.error(R.drawable.profile_placeholder)
.into(view)
}
// Conditional background from resource
android:background="@{state == 1 ? @drawable/shape_1 : @drawable/shape_2}"
// Formatted string
android:text="@{@string/remaining_km(data.remainingMeters / 1000)}"
<string name="remaining_km">%d km</string>
// Handle click
android:onClick="@{(v) -> viewModel.userDetail(v, viewModel.userId)}"
fun userDetail(view: View, userId: Int) {
showDetail(userId)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment