Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 527
Moses Aprico Avatar asked Oct 17 '25 10:10

Moses Aprico


2 Answers

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>
like image 70
Shaishav Avatar answered Oct 18 '25 23:10

Shaishav


Try changing the scaleType to android:scaleType="centerCrop", or some other scaleType.
See here:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

like image 41
amitairos Avatar answered Oct 19 '25 00:10

amitairos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!