Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Last active August 27, 2018 15:42
Show Gist options
  • Save SeongUgJung/5b16732dec26273ab41e46691b6abc70 to your computer and use it in GitHub Desktop.
Save SeongUgJung/5b16732dec26273ab41e46691b6abc70 to your computer and use it in GitHub Desktop.
drawable from resource
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<data>
<variable
name="vm"
type="MainViewModel"
/>
</data>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:drawableLeft="@{vm.textLeftImage}"
/>
</layout>
class MainViewModel {
val textLeftImage = ObservableInt(R.drawable.search)
}
@BindingAdapter("android:drawableLeft")
fun bindingVectorDrawableLeft(textView: TextView, resourceId: Int) {
val drawable = AppCompatResources.getDrawable(textView.context, resourceId)
val drawables = textView.compoundDrawables
textView.setCompoundDrawablesWithIntrinsicBounds(drawable,
drawables[1], drawables[2], drawables[3])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment