package com.xwray.vectorbinding; import android.databinding.BindingAdapter; import android.databinding.DataBindingUtil; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.graphics.drawable.VectorDrawableCompat; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); DataBindingUtil.setContentView(this, R.layout.activity_main); } /** * Unlike the support library app:srcCompat, this will ONLY work with vectors. * @param imageView * @param resourceId */ @BindingAdapter("android:src") public static void setImage(ImageView imageView, int resourceId) { Drawable drawable = VectorDrawableCompat.create(imageView.getResources(), resourceId, imageView.getContext().getTheme()); imageView.setImageDrawable(drawable); } /** * Unlike the support library app:srcCompat, this will ONLY work with vectors. * @param textView * @param resourceId */ @BindingAdapter("android:drawableLeft") public static void setDrawableLeft(TextView textView, int resourceId) { Drawable drawable = VectorDrawableCompat.create(textView.getResources(), resourceId, textView.getContext().getTheme()); Drawable[] drawables = textView.getCompoundDrawables(); textView.setCompoundDrawablesWithIntrinsicBounds(drawable, drawables[1], drawables[2], drawables[3]); } }