Skip to content

Instantly share code, notes, and snippets.

@yakuzaaaa
Last active August 11, 2016 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yakuzaaaa/ff1246050a8b391b7204b443dc734a8f to your computer and use it in GitHub Desktop.
Save yakuzaaaa/ff1246050a8b391b7204b443dc734a8f to your computer and use it in GitHub Desktop.
ModifiedCardViewCompat which removes dynamically the extra content padding added to cardviews in android on pre-lollipop devices
import android.content.Context;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.view.ViewGroup;
public class LemonadeCardView extends CardView {
public LemonadeCardView(Context context) {
super(context);
init();
}
public LemonadeCardView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LemonadeCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// Optional: Prevent pre-L from adding inner card padding
setPreventCornerOverlap(false);
// Optional: make Lollipop and above add shadow padding to match pre-L padding
setUseCompatPadding(true);
}
@Override
public void setLayoutParams(ViewGroup.LayoutParams params) {
// FIX shadow padding
if (params instanceof MarginLayoutParams) {
MarginLayoutParams layoutParams = (MarginLayoutParams) params;
layoutParams.bottomMargin -= (getPaddingBottom() - getContentPaddingBottom());
layoutParams.leftMargin -= (getPaddingLeft() - getContentPaddingLeft());
layoutParams.rightMargin -= (getPaddingRight() - getContentPaddingRight());
layoutParams.topMargin -= (getPaddingTop() - getContentPaddingTop());
}
super.setLayoutParams(params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment