oldcode
oldcode

Reputation: 1711

ImageView does not maintain constant height during orientation change

I have an XML layout which has an ImageView named cust_img, I might place an image of any dimension inside the ImageView but the height and the width must not change when the orientation changes. Currently this is not happening, could anyone suggest how could I achieve this?

I tried to apply the following ScaleType values

None of them worked.

Here is my layout XML:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="1"
            android:background="@color/white">

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.94"
                    android:fillViewport="true">

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

                        <LinearLayout
                            android:id="@+id/imglayout"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="0.3"
                            android:gravity="center_vertical"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/cust_img"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginBottom="10dp"
                                android:layout_marginLeft="20dp"
                                android:layout_marginRight="20dp"
                                android:layout_marginTop="10dp"
                                android:adjustViewBounds="true"
                                android:maxHeight="150dp"
                                android:maxWidth="160dp" />
                        </LinearLayout>

                        <FrameLayout
                            android:id="@+id/frag_layout"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="0.7"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
                        </FrameLayout>
                    </LinearLayout>
                </ScrollView>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.06">
                <include layout="@layout/footer_layout"/>
            </LinearLayout>
</LinearLayout>

Upvotes: 2

Views: 106

Answers (3)

Praneeth
Praneeth

Reputation: 1260

Instead of keeping width match parent use both, height and width wrap content.

Use image scale_type : CENTER_CROP.

    <ImageView
         android:id="@+id/cust_img"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginBottom="10dp"
         android:layout_marginLeft="20dp"
         android:layout_marginRight="20dp"
         android:layout_marginTop="10dp"
         android:adjustViewBounds="true"/>

Upvotes: 1

Bhargav Ghodasara
Bhargav Ghodasara

Reputation: 378

You have to put fix height to ImageView, otherwise give wrap_content to ImageView width.

Upvotes: 0

Anil
Anil

Reputation: 1087

Change your imageview code with this

                          <ImageView
                            android:id="@+id/cust_img"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="10dp"
                            android:layout_marginLeft="20dp"
                            android:layout_marginRight="20dp"
                            android:layout_marginTop="10dp"
                            android:adjustViewBounds="true"
                            android:maxHeight="150dp"
                            android:maxWidth="160dp" />

Upvotes: 0

Related Questions