Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if DrawableRes is VectorDrawable

I have a method that's setting VectorDrawable as DrawableLeft, DrawableRight, etc. on TextView and Button, but I would like to make it work also for normal Drawable. So, is there a way to check the type of DrawableRes, or should I use try/catch? The method looks like this:

public static void setViewDrawables(@NonNull View view, @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
    final Resources resources = view.getResources();
    Drawable vectorDrawableLeft = left != 0 ? VectorDrawableCompat.create(resources, left, view.getContext().getTheme()) : null;
    Drawable vectorDrawableTop = top != 0 ? VectorDrawableCompat.create(resources, top, view.getContext().getTheme()) : null;
    Drawable vectorDrawableRight = right != 0 ? VectorDrawableCompat.create(resources, right, view.getContext().getTheme()) : null;
    Drawable vectorDrawableBottom = bottom != 0 ? VectorDrawableCompat.create(resources, bottom, view.getContext().getTheme()) : null;

    if (view instanceof Button) {
        ((Button) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom);
    }
    else if (view instanceof TextView) {
        ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom);
    } else {
        Log.e("ERROR: ViewUtils", "Can't do setCompoundDrawablesWithIntrinsicBounds on " + view.getClass().getName());
    }
}
like image 654
Vladimir Jovanović Avatar asked Feb 23 '17 10:02

Vladimir Jovanović


1 Answers

To sum up comments to an answer. As far as I have found there is no easy way to know if DrawableRes is pointing to VectorDrawable or not. If someone knows how, please write. But to solve my problem it was enough to use android.support.v7.content.res.AppCompatResources as @pskink suggested. So now method looks like:

public static void setViewDrawables(@NonNull View view, @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
    Drawable vectorDrawableLeft = left != 0 ? AppCompatResources.getDrawable(view.getContext(), left) : null;
    Drawable vectorDrawableTop = top != 0 ? AppCompatResources.getDrawable(view.getContext(), top) : null;
    Drawable vectorDrawableRight = right != 0 ? AppCompatResources.getDrawable(view.getContext(), right) : null;
    Drawable vectorDrawableBottom = bottom != 0 ? AppCompatResources.getDrawable(view.getContext(), bottom) : null;

    if (view instanceof Button) {
        ((Button) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom);
    } else if (view instanceof TextView) {
        ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom);
    } else {
        Log.e("ERROR: ViewUtils", "Can't do setCompoundDrawablesWithIntrinsicBounds on " + view.getClass().getName());
    }
}

Update:

In android.support.v7.widget.AppCompatDrawableManager there is a method boolean isVectorDrawable(@NonNull Drawable d) which is checking if Drawable is VectorDrawable, but it's taking Drawable as an argument, and not DrawableRes. It looks like this:

private static boolean isVectorDrawable(@NonNull Drawable d) {
    return d instanceof VectorDrawableCompat
          || PLATFORM_VD_CLAZZ.equals(d.getClass().getName());
}
like image 133
Vladimir Jovanović Avatar answered Oct 16 '22 10:10

Vladimir Jovanović