Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different results of Android Support Library's CardView on different devices

Playing with Android Support Library v7 widget CardView I see different results on a Galaxy S4 comparing to a Nexus 4 device. Having the following layout:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="horizontal"
    card_view:cardCornerRadius="7dp"
    card_view:cardElevation="12dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true">

        <TextView
            android:id="@+id/txtExample"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/no_messages" />

    </ScrollView>

</android.support.v7.widget.CardView>

I got these results:

Nexus 4 (5.0.1): enter image description here

Samsung Galaxy S4 (4.4.2): enter image description here

It seems that the one on Nexus calculates the View with it's margins, and then the paints the shadow outside. On the other hand, the one of Samsung seems to apply margins and then paint the shadow inside until it reaches the calculated View's bounds.

Am I missing something?

like image 723
GoRoS Avatar asked Jan 26 '15 15:01

GoRoS


People also ask

Does cardview have padding on Android?

CardView with consistent look/UI on all Android platforms Android CardView have different padding/size on pre-Lollipop and post-Lolipop devices. On KitKat devices and above (API v21+), there is no outer padding between 2 CardView CardView adds additional padding to draw shadows on platforms before Lollipop.

What does the cardview library do?

This library adds support for the CardView widget, which lets you show information inside cards that have a consistent look on any app. These cards are useful for material design implementations, and are used extensively in layouts for TV apps. The Gradle build script dependency identifier for this library is as follows:

How do I use the support libraries in my Android SDK?

In order to use any of the following libraries, you must download the library files to your Android SDK installation. Follow the directions for downloading the Support Libraries in Support Library Setup to complete this step. You must take additional steps to include a specific Support Library in your application.

What are cardview and appcompatdialogfragment?

CardView - A support library custom class for creating Material Design style display cards. This class is based on FrameLayout with rounded corners and a drop shadow. AppCompatDialogFragment - Provides a consistently styled dialogs by extending DialogFragment and using AppCompatDialog .


1 Answers

All your observations are correct :)
Everything is explained well on official documentation of CardView:

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.

and:

Note that, if you specify exact dimensions for the CardView, because of the shadows, its content area will be different between platforms before L and after L. By using api version specific resource values, you can avoid these changes. Alternatively, If you want CardView to add inner padding on platforms L and after as well, you can set setUseCompatPadding(boolean) to true.

As described there - you should just use setUseCompatPadding (true) then outer padding on both: L and pre-L will be the same.

like image 60
Maciej Ciemięga Avatar answered Sep 20 '22 19:09

Maciej Ciemięga