How do I remove extra space above and below imageView?
AndroidImageviewAndroid Problem Overview
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:
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>
Android Solutions
Solution 1 - Android
Try this
android:adjustViewBounds="true"
Solution 2 - Android
In your above imageView
just add android:scaleType="fitXY"
Solution 3 - Android
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](http://developer.android.com/reference/android/widget/ImageView.ScaleType.html "the XML syntax is there as well").
<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" />
Solution 4 - Android
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" />
Solution 5 - Android
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"/>
Solution 6 - Android
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.
Solution 7 - Android
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"/>
Solution 8 - Android
just add ScaleType="fitxy" inside the Image view
Solution 9 - Android
Try this
<ImageView
(...)
android:adjustViewBounds="true" />
Solution 10 - Android
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.
Solution 11 - Android
this line of code will solve the problem
android:adjustViewBounds="true"
don't forget to rotate the image