86

I have a really simple image within a RelativeLayout and for some reason I am getting extra spacing on the top and bottom which I can't remove. How can I clear it out?

Here is what it looks like:

Image from Eclipse preview

Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/slice11pp" />
</RelativeLayout>
honk
  • 9,137
  • 11
  • 75
  • 83
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63

11 Answers11

289

Try this

android:adjustViewBounds="true"

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Sagar V.
  • 3,567
  • 1
  • 19
  • 14
  • in my code this work only with 'android:layout_width="match_parent" android:layout_height="match_parent"' – Tapa Save Jan 26 '17 at 12:43
10

In your above imageView just add android:scaleType="fitXY"

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Kosh
  • 6,140
  • 3
  • 36
  • 67
5

Your problem is probably that you have no scale type set... Add it to the XML for the ImageView, "fitCenter" should be correct, but there are others, check: ScaleType.

<ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/slice11pp"
            android:scaleType="fitCenter" />
Tonithy
  • 2,300
  • 1
  • 20
  • 20
  • 1
    In my case, I also needed to set android:adjustViewBounds="true", as its explained in this question: http://stackoverflow.com/questions/9701685/empty-space-above-imageview?rq=1 – AlvaroSantisteban Dec 10 '13 at 22:38
5

Must be enough adding the property:

android:adjustViewBounds="true"

for example:

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/slice11pp"
        android:adjustViewBounds="true" />

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
3

If your image view height is match_parent, even if you set android:adjustViewBounds="true" ImageView will add some extra blank space at top and bottom side. so, change ImageView height to wrap_content and set android:adjustViewBounds="true"

for example

<ImageView
  android:id="@+id/img_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"/>
Amol Suryawanshi
  • 2,108
  • 21
  • 29
3

Use drawable-nodpi folder if there is no specific requirement for images. Then android: adjustViewBounds = "true" acts as the default.

If you use drawable-nodpi you don't need to set android:adjustViewBounds = "true".

I think this is the most effortless method.

Mahmut K.
  • 707
  • 1
  • 8
  • 20
2

android:scaleType="centerCrop" by adding this

<ImageView
    android:id="@+id/image"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@drawable/temp_image"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop"/>

works for me Before and After Image Before and After Image

Akhila
  • 3,235
  • 1
  • 14
  • 30
2

this line of code will solve the problem

android:adjustViewBounds="true"

don't forget to rotate the image

Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
0

just add ScaleType="fitxy" inside the Image view

0

Try this

<ImageView
     (...)
     android:adjustViewBounds="true" />
Pankaj Talaviya
  • 3,328
  • 28
  • 31
0

this is the perfect solution

 <ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>

it will not leave any extra single dp in any side. however the image will be distorted if it is not portrait image.