paxx
paxx

Reputation: 1079

ImageView with background leaves margin when image is loaded

I have my imageview declared like this:

<ImageView
        android:id="@+id/category_image_top"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:maxHeight="170dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:background="@drawable/image_placeholder"
        />

And this is how I set my category_image_top in asynctask (on onPostExecute)

imageView.setImageBitmap(image);

When image is set, imageview suddenly gets a margin of 4px. But when I remove android:background="@drawable/image_placeholder" from XML everything is fine?!

BTW: image_placeholder is a 9-patch image, if this makes any difference.

Any ideas why this happens?

UPDATE: I've tried placing background as a solid color and then no margins appear when image is loaded. I've also tried placing another 9-patch image and when I do so margins appear again. So it must be something with background as an image

UPDATE2: Maybe it's an android bug like this guy points out? https://stackoverflow.com/a/8340745/581531

Upvotes: 4

Views: 5282

Answers (3)

paxx
paxx

Reputation: 1079

OK. Solution was found, maybe it's not the prettiest one but hey, it work! :)

In my view I've just added an arbitrary View that is a holder for my image. On top of it i have my ImageView so when the image is loaded placeholder (View) get covered. This is my view (or at least part that matters):

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

        <View 
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:background="@drawable/image_placeholder"
            />

        <ImageView
            android:id="@+id/article_image"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:adjustViewBounds="true" 
            android:layout_gravity="center"
            android:scaleType="centerCrop"
            />
...

Upvotes: 1

Todd Davies
Todd Davies

Reputation: 5522

The element that contains the image view may have a margin, have you checked that?

Change android:background="@drawable/image_placeholder" to android:src="@drawable/image_placeholder"

Also add the following: android:scaleType="centerCrop"

If centerCrop doesn't work, try the other ones listed here.

Upvotes: 0

logcat
logcat

Reputation: 3454

I can guess that image_placeholder has 4px transparent margin

Upvotes: 1

Related Questions