2

I have a parent LinearLayout inside which I have 2 CardViews. I have an ImageView inside each CardView. When I load image in any of ImageView its not taking the complete width of the CardView. There is a slight white gap between the ImageView and CardView. How can I overlap the ImageView completely over the CardView? Here is my layout.

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#F5F5F5"
        android:gravity="center">

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

            <android.support.v7.widget.CardView
                android:id="@+id/adhaarCardFrontUpload"
                android:layout_width="0dp"
                android:layout_height="@dimen/adhaar_height"
                android:layout_marginBottom="5dp"
                android:layout_marginEnd="5dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                app:cardCornerRadius="5dp"
                app:cardPreventCornerOverlap="false"
                app:cardElevation="2dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:gravity="center"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:adjustViewBounds="true"
                            android:scaleType="fitXY"
                            android:src="@drawable/ic_add" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="2dp"
                            android:gravity="center"
                            android:padding="2dp"
                            android:text="@string/upload_adhaar_front"
                            android:textColor="@color/colorAccent" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/adhaarCardFrontImage"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:adjustViewBounds="true"
                        android:scaleType="centerCrop"
                        android:visibility="visible" />

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

            <android.support.v7.widget.CardView
                android:id="@+id/adhaarCardBackUpload"
                android:layout_width="0dp"
                android:layout_height="@dimen/adhaar_height"
                android:layout_marginBottom="5dp"
                android:layout_marginEnd="10dp"
                android:layout_marginStart="5dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                app:cardPreventCornerOverlap="false"
                app:cardCornerRadius="5dp"
                app:cardElevation="2dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:gravity="center"
                        android:orientation="vertical">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:adjustViewBounds="true"
                            android:scaleType="fitXY"
                            android:src="@drawable/ic_add" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="2dp"
                            android:gravity="center"
                            android:padding="2dp"
                            android:text="@string/upload_adhaar_back"
                            android:textColor="@color/colorAccent" />
                    </LinearLayout>

                    <ImageView
                        android:id="@+id/adhaarCardBackImage"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:adjustViewBounds="true"
                        android:scaleType="centerCrop"
                        android:visibility="visible" />

                </RelativeLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </LinearLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sagar suri
  • 4,351
  • 12
  • 59
  • 122

2 Answers2

2

Change adjustViewBounds to false.

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:scaleType="fitXY"
android:src="@drawable/ic_add" />

If you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable then set this to true.

Ajay J G
  • 886
  • 7
  • 21
  • Actually I am talking about the `ImageView` with `id="adhaarCardBackImage"`. I have it as match_parent but still it doesn't take the complete width of the parent `CardView` – sagar suri Apr 01 '18 at 16:34
  • Did you try toggling adjustviewbounds? – Ajay J G Apr 01 '18 at 16:36
0

You should use MATCH_PARENT.

Special value for the height or width requested by a View. MATCH_PARENT means that the view wants to be as big as its parent, minus the parent's padding, if any.

Try this way,

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:src="@drawable/ic_add" 
  />

Try with

 android:adjustViewBounds="true"
 android:scaleType="fitCenter"
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198