androidimageviewcardview

How to get rid of blank spaces around an ImageView inside a CardView


Below screenshot, I am inserting an image, using ImageView, into a CardView.

The problem is, I seem can't get rid of the empty space below the Image. (highlighted by light blue)

I tried a solution that works, but it requires me to explicitly specify the image height, which I try not to, because, the source can be various, and I need the image to flexibly scale up/down (with same aspect ratio) depending on the card width.

Here's the screenshot:

enter image description here

And here's the code snippet:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="2dp"
    card_view:cardElevation="2dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="7dp">
        <TextView
            android:id="@+id/caption_text"
            android:text="Hello World!"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:scaleType="fitStart"
            android:layout_below="@id/caption_text"
            android:src="@drawable/food"/>
    </RelativeLayout>
</android.support.v7.widget.CardView>

So, is there any way to make the CardView automatically wrap around the image, (without leaving any empty space) and without specifying the image height explicitly?


Solution

  • Change the <RelativeLayout> height to wrap_content. Currently, it has a circular dependency in height.

    [EDIT]

    We need to set the property android:adjustViewBounds="true" for the image to scale with the source. Its false by default!

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="2dp"
        card_view:cardElevation="2dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="7dp">
            <TextView
                android:id="@+id/caption_text"
                android:text="Hello World!"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:adjustViewBounds="true"
                android:layout_below="@id/caption_text"
                android:src="@drawable/food"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>