javaandroidandroid-animationandroid-cardviewtransitiondrawable

CardView doesn't support TransitionDrawable?


I tried to animate a switch-color on CardView background, but I'm getting this:

Cannot resolve method 'setCardBackgroundColor(android.graphics.drawable.TransitionDrawable)'

If CardView does not support TransitionDrawable, then how can we achieve something like this?

public void setCardBackground(CardView cardView) {
    ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
    TransitionDrawable trans = new TransitionDrawable(color);
     //cardView.setCardBackgroundColor(trans);
    trans.startTransition(5000);
}

Solution

  • Have you tried View#setBackground()?

    public void setCardBackground(CardView cardView) {
        ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
        TransitionDrawable trans = new TransitionDrawable(color);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            cardView.setBackground(trans);
        } else {
            cardView.setBackgroundDrawable(trans);
        }
        trans.startTransition(5000);
    }