sagar suri
sagar suri

Reputation: 4731

ImageView not covering complete width of the parent CardView

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>

Upvotes: 2

Views: 1222

Answers (2)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

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"

Upvotes: 0

Ajay J G
Ajay J G

Reputation: 926

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.

Upvotes: 3

Related Questions