Data Binding with srcCompat
Solution 1:
You can simply use android:src attribute instead compat attribute when you set vector resource by DataBinding.
DataBinding library generates class that execute setImageResource method at runtime.
<ImageView
...
android:src="@{@drawable/your_drawable}"
/>
According to http://android-developers.blogspot.com/2016/02/android-support-library-232.html setImageResource method can be used at runtime on older system versions without any additional changes.
If you would like to use app:srcCompat attribute. You must define @BindingMethods annotation which connects attribute with appropriate setter from ImageView. For example in your Activity or Fragment add.
@BindingMethods({
@BindingMethod(type = android.widget.ImageView.class,
attribute = "app:srcCompat",
method = "setImageDrawable") })
public class MainActivity extends AppCompatActivity {
// your activity body here
}
Solution 2:
You may have to resort to using a binding adapter with a method signature similar to the following:
@BindingAdapter("app:srcCompat")
public static void bindSrcCompat(ImageView imageView, Drawable drawable){
// Your setter code goes here, like setDrawable or similar
}
Here is the reference: http://developer.android.com/reference/android/databinding/BindingAdapter.html
Solution 3:
The proposed answers mainly worked for me but I also needed to add this line in my Application:
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
Doing so allows me to use vector drawables in older versions without needing to worry about compat classes or attributes