Elementary
Elementary

Reputation: 2153

How to resize an ImageView within a given Layout in Android?

I have a problem to make a proper layout for a special case. I experimented on that already for a while both in the designer and in code, but I couldn't find a solution, that's why I need your help.

I have to create a layout which should have a structure like pictured in the images below. It is mainly a combination of several linearLayouts. The problem I have is, that the picture can only be added within the code, because this layout is a detail view that displays information about items from a list.

top: layout without loaded image; middle: current state with loaded image; bottom: ideal state for loaded image

Question: How can I get a layout (after the image is loaded in code) that looks like the bottom picture? The image, after loaded in code, has to resize itself, so it uses the whole available height and resizes its width accordingly. The "relativeLayout_Top" and the "linearLayout_BelowImage" have both fixed heights. The "scrollView_BigRight" adjusts itself based on the space that the "imageView_OrangeImage" doesn't need for itself.

I can deal with solutions that adjust the layout in code, after the image has been added, or solutions that makes the layout.xml itself flexilbe enough to deal with this situation.

Any help is highly appreciated. If you need any more information please let me know.

Below is the main content of my layout.xml, that is needed for this problem.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/white">

    <RelativeLayout
        android:id="@+id/relativeLayout_Top"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/blue" >
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/linearLayout_Big"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:background="@color/transparent" >

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

            <ImageView
                android:id="@+id/imageView_OrangeImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/black" />

            <LinearLayout
                android:id="@+id/linearLayout_BelowImage"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/blue_white_blue" >

                <Button
                    android:id="@+id/btn1"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/blue" />

                <TextView
                    android:id="@+id/textView_BelowImageMiddle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@color/white" />

                <Button
                    android:id="@+id/btn2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/blue" />
            </LinearLayout>
        </LinearLayout>

        <ScrollView
            android:id="@+id/scrollView_BigRight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/grey" >
        </ScrollView>
    </LinearLayout>
</LinearLayout>

Upvotes: 0

Views: 3260

Answers (1)

Kirill Shalnov
Kirill Shalnov

Reputation: 2216

android:adjustViewBounds="true"

This one’s a manual fix for “optimized” code in scaleType="fitCenter". Basically when Android adds an image resource to the ImageView it tends to get the width & height from the resource instead of the layout. This can cause layouts to reposition around the full size of the image instead of the actual viewable size.

AdjustViewBounds forces Android to resize the ImageView to match the resized image prior to laying everything else out. There are times where this calculation won’t work, such as when the ImageView is set to layout_width="0dip". If it’s not working, wrap the ImageView in a RelativeLayout or FrameLayout which handles the 0dip flexible size instead

get it from this site

OR

Mode android:scaleType="centerCrop" uniformly stretches the image to fill the entire container and trims unnecessary.

You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself!

get it here

Upvotes: 3

Related Questions