1

I am trying to put the ImageView on the bottom of the layout with following XML:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            tools:context=".MainActivity" >
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#EEEEEE"
                android:gravity="center"
                android:orientation="horizontal" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:src="@drawable/photo" />
            </LinearLayout>
        </LinearLayout>

I am following the instructions found here http://sandipchitale.blogspot.co.uk/2010/05/linearlayout-gravity-and-layoutgravity.html

Only difference is that I use ImageView instead of Button as here:

<?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" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="bottom" >
        </Button>
    </LinearLayout>
</LinearLayout>

Which puts the button on the expected place:

enter image description here

Unfortunately this isn't the case in my XML on the top of the post. The ImageView isn't placed on the bottom as the Button in reference XML is.

enter image description here

Any ideas why that happens ?

Matko
  • 3,386
  • 4
  • 21
  • 35

5 Answers5

7

You should change a setting in ImageView itself, to be precise set android:scaleType="fitEnd" on your ImageView.

The other answers seem to be workarounds, including your own answer. This is not a problem with a layout, but with where inside the ImageView the image is loaded when the dimensions of the image and the screen aren't similar, (i.e. the width/height ratio is very different). By default it would load the image in the middle of the ImageView.

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73
  • This is the best answer. Thank you. from Android documentation "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. END aligns the result to the right and bottom edges of dst. " [here](https://developer.android.com/reference/android/graphics/Matrix.ScaleToFit#END) – A.R.H Oct 04 '18 at 08:59
5

Try using RelativeLayout instead of LinearLayout and use parameter android:layout_alignParentBottom = "true".

EDIT: Try giving layout_height and layout_width as wrap_content for your LinearLayout that holds your ImageView.

Or it might be that the image you are using has extra space above and below it.

UPDATE:

The problem is with the size of the image. It is much larger than the screen size. I would suggest you to programmatically read the screen size of the device with below code:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

and set the height and width for imageView programmatically as below:

image_view.getLayoutParams().height = height - 50;
image_view.getLayoutParams().width = width - 50;
1

Try this inside relative layout

<ImageView
    android:id="@+id/imageView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:adjustViewBounds="?attr/isLightTheme"
    app:srcCompat="@drawable/client_app_footer" />

tested & working as expected

Rohith M
  • 31
  • 1
  • 6
0

you did not include android:layout_gravity="bottom" line in your imageview.

rachit
  • 1,981
  • 15
  • 23
  • I forgot to paste it at first. I have updated the question - added the _layout-gravity="botttom"_ line. But still the effect is the same. Image isn't at the bottom of the display. – Matko Oct 06 '13 at 17:25
  • 2
    You can use RelativeLayout instead of Linear and then use android:layout_alignParentBottom="true" to align image at bottom. – rachit Oct 06 '13 at 17:28
0

I have resized photo size from width 2560 to width 270 and finally ImageView went to bottom.

Matko
  • 3,386
  • 4
  • 21
  • 35