Skip to content Skip to sidebar Skip to footer

Card View Not Proper In 5.1.1 Tablet And 5.0.2 Mobile Device

I am having the following view created with CardView .Adding dependency compile 'com.android.support:cardview-v7:23.0.+' in gradle dependency Below is the xml file for the same. &l

Solution 1:

Try removing: android:padding attribute from the card view.

from the docs:

Since padding is used to offset content for shadows, you cannot set padding on CardView. Instead, you can use content padding attributes in XML or setContentPadding(int, int, int, int) in code to set the padding between the edges of the Card and children of CardView.

Also this:

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.

Solution 2:

Finally I figured out the issue. It was due to the outer LinearLayout I had written to make the icons in center of screen which was having height as wrap_content. I avoided the LinearLayout and through the following code, I made the card view with proper elevation curves and in middle .

<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/mainRL"
        ><android.support.v4.widget.Spaceandroid:layout_width="@dimen/margin_10"android:layout_height="1dp"android:layout_centerInParent="true"android:layout_centerVertical="true"android:id="@+id/spaceCenter"
                /><!-- Employee icon --><android.support.v7.widget.CardViewxmlns:card_view="http://schemas.android.com/apk/res-auto"android:id="@+id/cardViewEmp"android:layout_height="wrap_content"android:layout_width="wrap_content"card_view:cardCornerRadius="20dp"card_view:cardElevation="10dp"android:layout_toLeftOf="@+id/spaceCenter"android:layout_centerInParent="true"
                ><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"
                    ><ImageViewandroid:id="@+id/employeeIcon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingTop="@dimen/margin_5"android:src="@drawable/employeeicon"android:layout_centerHorizontal="true"
                    /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="  Employee  "android:layout_centerHorizontal="true"android:layout_below="@+id/employeeIcon"android:textSize="@dimen/textSizeNormal"
                    /></RelativeLayout></android.support.v7.widget.CardView><!-- Vehicle icon --><android.support.v7.widget.CardViewxmlns:card_view="http://schemas.android.com/apk/res-auto"android:id="@+id/cardViewVehicle"android:layout_height="wrap_content"android:layout_width="wrap_content"card_view:cardCornerRadius="20dp"card_view:cardElevation="10dp"android:layout_toRightOf="@+id/spaceCenter"android:layout_centerInParent="true"
                ><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"
                    ><ImageViewandroid:id="@+id/vehicleIconLive"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingTop="@dimen/margin_5"android:src="@drawable/vehicleicon"android:layout_centerHorizontal="true"
                        /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="    Vehicle     "android:layout_centerHorizontal="true"android:layout_below="@+id/vehicleIconLive"android:textSize="@dimen/textSizeNormal"
                        /></RelativeLayout></android.support.v7.widget.CardView></RelativeLayout>

Solution 3:

To make it both compatible above or below api 21, you need to specify app:cardUseCompatPadding="true" in your support CardView.

credit to ShinChven CardView elevation not working on Android 5.1.1

Post a Comment for "Card View Not Proper In 5.1.1 Tablet And 5.0.2 Mobile Device"