Cardview corner background not transparent

Use the following code, on a place where the context is available, in your custom implementation:

setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent));

EDIT:

Use the following code for android versions lower than Lollipop to avoid the mentioned crash.

  if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
     getBackground().setAlpha(0);
  } else {
     setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent);
  }

After a few years I came to the conclusion that I was making a fundamental mistake when creating compound components.

When making a compound component that extends android.support.v7.widget.CardView the layout xml that's inflated shouldn't also contain a CardView. Basically you'll just be adding a CardView to another CardView which will result in the white corners behind my custom CardView.

The accepted answer will solve this problem. However it's not the perfect solution.

There are 2 ways to fix the real problem:

  1. The compound view extends android.support.v7.widget.CardView and the layout xml doesn't contain android.support.v7.widget.CardView but a LinearLayout instead as the root. Styling will have to handled in the class.
  2. The compound view extends LinearLayout and the layout xml contains android.support.v7.widget.CardView as the root.

I chose the 1st solution to fix this problem. I hope this helps people that made the same mistake.