Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vector drawables from XML with the Android support library 23.3.0
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="com.xwray.vectorbinding.R"/>
</data>
<LinearLayout
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xwray.vectorbinding.MainActivity">
<TextView
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
tools:drawableLeft="@drawable/ic_star"
android:drawableLeft="@{R.drawable.ic_star}"
android:text="Hello World!"/>
<ImageView
android:src="@{R.drawable.ic_star}"
tools:src="@drawable/ic_star"
android:tint="@color/colorAccent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
</layout>
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.xwray.vectorbinding"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
}
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]);
}
}
@rpattabi

This comment has been minimized.

Copy link

@rpattabi rpattabi commented Apr 12, 2016

Awesome. Thanks.

@subhasha1

This comment has been minimized.

Copy link

@subhasha1 subhasha1 commented Apr 25, 2016

I have some problem:

@BindingAdapter("app:vectorDrawableLeft")
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]);
}

I used in edittext as xml file:

          <EditText
                    ...
                    android:background="@android:drawable/editbox_background"
                    android:drawablePadding="4dp"
                    app:vectorDrawableLeft="@{R.drawable.ic_search_black_24dp}" />

And image is not aligned in center in devices with api 23 while with lower api devices it seems fine.
screen shot 2016-04-25 at 7 41 26 pm

In my opinion it makes lot of sense that it is not related to databinding, have you have encountered this issue?

@httpdispatch

This comment has been minimized.

Copy link

@httpdispatch httpdispatch commented Apr 26, 2016

Thanks for sharing the solution. Do you have any idea how to keep using StateListDrawable with vectors?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_vector_disabled" android:state_enabled="false"/>
    <item android:drawable="@drawable/button_vector_pressed" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_vector_enabled"/>
</selector>
@eudson

This comment has been minimized.

Copy link

@eudson eudson commented Jul 26, 2016

This applies to animated-vector 2?

@MichaelRocks

This comment has been minimized.

Copy link

@MichaelRocks MichaelRocks commented May 12, 2017

Seems I've found a simpler solution: https://github.com/MichaelRocks/DataBindingCompat
BTW, this approach doesn't work when using a ternary operator with drawable resources in a binding expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment