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

Moses Aprico

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?

Shaishav

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jquery/javascript how to get rid of blank spaces (replace() not working)

From Dev

How to Get Rid of These Extra Spaces?

From Dev

Python: how to get rid of spaces in str(dict)?

From Dev

How can I get rid of "white spaces"

From Dev

How to get rid of spaces on mobile version

From Dev

How can I get rid of blank spaces in missing values when export to a file 2 dataset with 2 different structures

From Dev

How to get maps rendered inside of a CardView

From Dev

Button has a weird border around it, how do I get rid of it?

From Dev

How to get rid of the space around buttons in Nimbus LAF?

From Dev

how to get rid of default white "space" around an element?

From Dev

How do I get rid of the white outline around my button?

From Dev

Android: how to get rid of extra space in ImageView's bounding box

From Dev

How to get rid of the blank parts from the sides of a container in bootstrap

From Dev

How to get rid of the unexpected spaces in H1 block

From Dev

How do I get rid of trailing and embedded spaces in a string?

From Dev

How to get rid transparent spaces between connecting elements?

From Dev

How can I get rid of trailing spaces in vim without breaking &?

From Dev

Spaces in the "Word" began to be replaced by dots. How to get rid of them?

From Dev

How to get rid transparent spaces between connecting elements?

From Dev

How to get rid of additional spaces using redcarpet gem (Ruby on Rails)

From Dev

How to get rid of special characters but still keep spaces in formatting a string?

From Dev

How to add a blank spaces before and after the content inside a layer?

From Dev

Get rid of enter spaces in a sentence

From Dev

Why does ImageView fades inside CardView?

From Dev

How to get range of cells in one cell separated by two blank spaces

From Dev

How to get reference to a View inside CardView from an activity?

From Dev

Akka. How to get rid of business logic inside actors?

From Dev

How to get rid of underline on <span> inside <a> tag with hover?

From Dev

How to get rid of folder inside eclipse Referenced Libraries

Related Related

  1. 1

    jquery/javascript how to get rid of blank spaces (replace() not working)

  2. 2

    How to Get Rid of These Extra Spaces?

  3. 3

    Python: how to get rid of spaces in str(dict)?

  4. 4

    How can I get rid of "white spaces"

  5. 5

    How to get rid of spaces on mobile version

  6. 6

    How can I get rid of blank spaces in missing values when export to a file 2 dataset with 2 different structures

  7. 7

    How to get maps rendered inside of a CardView

  8. 8

    Button has a weird border around it, how do I get rid of it?

  9. 9

    How to get rid of the space around buttons in Nimbus LAF?

  10. 10

    how to get rid of default white "space" around an element?

  11. 11

    How do I get rid of the white outline around my button?

  12. 12

    Android: how to get rid of extra space in ImageView's bounding box

  13. 13

    How to get rid of the blank parts from the sides of a container in bootstrap

  14. 14

    How to get rid of the unexpected spaces in H1 block

  15. 15

    How do I get rid of trailing and embedded spaces in a string?

  16. 16

    How to get rid transparent spaces between connecting elements?

  17. 17

    How can I get rid of trailing spaces in vim without breaking &?

  18. 18

    Spaces in the "Word" began to be replaced by dots. How to get rid of them?

  19. 19

    How to get rid transparent spaces between connecting elements?

  20. 20

    How to get rid of additional spaces using redcarpet gem (Ruby on Rails)

  21. 21

    How to get rid of special characters but still keep spaces in formatting a string?

  22. 22

    How to add a blank spaces before and after the content inside a layer?

  23. 23

    Get rid of enter spaces in a sentence

  24. 24

    Why does ImageView fades inside CardView?

  25. 25

    How to get range of cells in one cell separated by two blank spaces

  26. 26

    How to get reference to a View inside CardView from an activity?

  27. 27

    Akka. How to get rid of business logic inside actors?

  28. 28

    How to get rid of underline on <span> inside <a> tag with hover?

  29. 29

    How to get rid of folder inside eclipse Referenced Libraries

HotTag

Archive