user8601021 user8601021 - 1 year ago 128
Android Question

unneccesary padding on top and bottom of cardview in recyclerview

I have a cardview in a recyclerview and the problem I am facing is that on populating the recyclerview unneccessary padding appears on top and bottom of imageview inside the cardview.The code of the recyclerview row is given belowscreenshot.please help me
I have tried a lot of codes on stack but no use

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_margin="5dp"
card_view:cardCornerRadius="5dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/productsearch_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/productsearch"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Proin Volutpat Purus."
android:textSize="11sp"
android:scaleY="1.2"
android:layout_weight="2"
android:layout_gravity="center"
android:textColor="@android:color/black"
android:textStyle="bold"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="$"
android:layout_gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:textColor="@color/textcolorpink"/>

<TextView
android:id="@+id/prodprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:text="25"
android:textColor="@color/textcolorpink"
android:textSize="20sp"
android:textStyle="bold" />

</LinearLayout>


</LinearLayout>



Answer Source

Try add

android:adjustViewBounds="true"

to the ImageView

<ImageView
        android:id="@+id/productsearch_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/productsearch"/>

P.S. Why do you use LinearLayout for ImageView, you can remove it. It's an unnecessary nesting

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download