Home:ALL Converter>android:layout_height not working for CardView

android:layout_height not working for CardView

Ask Time:2015-01-04T03:03:09         Author:Umer Farooq

Json Formatter

I am working with CardViews inside a RecyclerView with GridLayoutManager. The problem that I am facing is, the height I specify for the card view inside xml or in java, it doesn't get enforced by the CardView. It seems like the cumulative height of the child views of the CardView becomes the height of the CardView. This behavior considering the fact that it's parent's layout parameters that are enforced on its child views.

What am I missing? Am I doing something wrong?

<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/cardView"
    android:layout_width="@dimen/card_view_width"
    android:layout_height="0dp"
    android:layout_marginBottom="6dp"
    android:elevation="10dp"
    android:orientation="horizontal"
    card_view:cardCornerRadius="2dp"

    >

<ImageView
 android:layout_width="@dimen/card_view_width"
    android:layout_height="10dp"
></ImageView>

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

In this case the height of the card view is 0dp but still the layout designer preview & when the app runs on the device, the size of the card is 10dp.

Best Regards

Author:Umer Farooq,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27758479/androidlayout-height-not-working-for-cardview
Alex K :

What do you expect to happen, with what you told the program?\n\nYou tell the CardVeiw to have a height of 0dp with this:\n android:layout_height=\"0dp\"\n\nThen you place an ImageView inside, and tell it to have a height of 10dp. \n\nSo of course the CardView makes itself taller to allow the ImageView to fit properly.\n\nIf you want the inner elements to match the height of the CardView, set the layout_height of the CardView to whatever you want, like 20dp:\n\nandroid:layout_height=\"20dp\"\n\n\nThen have the inner elements match_parent:\n\nandroid:layout_height=\"match_parent\"\n",
2015-01-03T20:28:32
yigit :

Why are you setting visibility to 0? IF you are trying to hide it, use `visibility = View.GONE'.\n\nALso, don't use elevation which won't work before L. Insteed, use cardElevation so that it will work before L as well.",
2015-01-04T09:32:02
yy