Search code examples
androidandroid-imageview

ImageView - Unwanted padding at left and right


I have an ImageView inside a ScrollView. The width and height of the ImageView and match_parent and wrap_content. But, the ImageView has slight padding on the left and right to it. The image is downloaded from server and shown in the ImageView.

Here is my XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.xx.xxx"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar_without_spinner" />

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/ll_select_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/green"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/ll_feature"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <LinearLayout
                    android:id="@+id/text_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingBottom="10dp"
                    android:paddingTop="10dp"
                    android:visibility="gone" >

                    <TextView
                        android:id="@+id/item_features_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/empty"
                        android:textAppearance="?android:attr/textAppearanceLarge" />

                    <TextView
                        android:id="@+id/item_features_description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="@string/empty"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
                    <!-- ImageView with the problem -->
                    <ImageView
                        android:id="@+id/item_features_imageview_icon"
                        android:layout_width="0dip"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:padding="0dp"
                        android:contentDescription="@string/item_features_icon"
                        android:cropToPadding="false"
                        android:scaleType="fitCenter" />
                </LinearLayout>
            </LinearLayout>

           ....
           .... 
        </LinearLayout>
    </ScrollView>

</LinearLayout>

Please find the screenshots below:

enter image description here

enter image description here

How do i make the ImageView match the total width of the scrollView and also maintain the aspect ratio?

Shouldn't "fitCenter" automatically match the edges, as per documentation?

ImageView.ScaleType. FIT_CENTER

Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.


Solution

  • Use this

    android:scaleType="fitX"
    

    It will expand your image in horizontal removing the extra padding

    You can also use

    android:scaleType="fitXY"
    

    It will expand both in vertical as well as horizontal