Last active
August 11, 2016 11:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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