Cardview Has Extra Margin In Each Edge On Pre-lollipop
Solution 1:
So here it goes perfectly fine on Kitkat, samsung device to be precise.
I tried card_view:cardUseCompatPadding="true"
but no avail. Didn't work!
Then I discovered from a stackoverflow post this
card_view:cardPreventCornerOverlap="false"
and VOILA! Worked! There were no round corners (Since, I wanted none as the Card has an Image background).
The moral is, an extra padding is because of those tiny round corners which, need to be disabled. Basically that is not a flaw rather a Design constraint!
Image: Notice that top corners are edges (A view which has colour and an image in background) while bottom has just TextViews and no backgrounds hence, the round corners. That means if a view is requesting match_parent
inside the CardView, card_view:cardPreventCornerOverlap="false"
will allow that to be taken up, on the affected corners.
Solution 2:
Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.
From the CardView reference here
Try setting a negative left margin on the CardView
like this
<android.support.v7.widget.CardView
android:id="@+id/card_title_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:cardCornerRadius="0dp"
app:cardBackgroundColor="@color/colorAccent"
app:cardUseCompatPadding="true"
android:layout_marginLeft="-2dp" />
You may need to adjust the margin to get the desired result.
PS, this is kind of a hack-y way to do it.
Solution 3:
I realise this has already been answered, but I'd like to add that in addition to card_view:cardPreventCornerOverlap="false"
, I also had to set CardView.setMaxCardElevation(0)
to get rid of the margins on pre-Lollipop. Setting the elevation only to 0 did not work. I'm using support library v23.4.0.
Solution 4:
Try with card_view:cardUseCompatPadding="true"
if we set this property to true then margin works same on all versions.
developer note
Add padding in API v21+ as well to have the same measurements with previous versions.
source docs
Solution 5:
If the Android 4 (pre-lollipop) behavior is desired, adding app:cardUseCompatPadding="true"
to the CardView
should fix it.
If the Android 5+ behavior is desired (which is actually the right behavior of card views according to material guidelines), the exact same thing can not be easily achieved. I usually use this fix to avoid defining multiple layout files and have reasonable output on all devices:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentPaddingRight="@dimen/fix_cardview"
app:contentPaddingLeft="@dimen/fix_cardview"
app:contentPaddingTop="@dimen/fix_cardview_vertical"
app:contentPaddingBottom="@dimen/fix_cardview_vertical" />
and in normal values/dimens.xml
file we should have:
<dimenname="fix_cardview">-8dp</dimen><dimenname="fix_cardview_vertical">-12dp</dimen>
and in values-v21/dimens.xml
:
<dimenname="fix_cardview">0dp</dimen><dimenname="fix_cardview_vertical">0dp</dimen>
note that the numbers -8dp
and -12dp
might have to be adjusted for your layout, since they depend on elevation, etc.
This is only a work-around to avoid ugly paddings in Android 4 views, without using different views in different layout files (which usually makes the code more difficult to maintain)
Post a Comment for "Cardview Has Extra Margin In Each Edge On Pre-lollipop"