toadzky
toadzky

Reputation: 261

Android GridView Items Resizing

I using a GridView to display a feed. If the entry has a background image for it, the image gets loaded from the device or the server. My problem is that when an image is loaded, the gridview's item expands to accomodate the image. I want the image to scale down to fit the box.

I have tried using an ImageView with various scaletypes and using the view's background. Nothing seems to work. Any ideas?

Upvotes: 1

Views: 3306

Answers (2)

toadzky
toadzky

Reputation: 261

The answer I found was that when I was inflating the XML is I was using:

LayoutInflater.from(context).inflate(R.layout.item, null)

instead of

LayoutInflater.from(context).inflate(R.layout.item, container, false);

If you use the first, the inflated view and the container don't communicate, so the "layout_XXX" attributes for the view are ignored.

Upvotes: 5

Daniel Maclean
Daniel Maclean

Reputation: 789

Gridview just like any other layout needs boundaries defined otherwise it will adjust to the content. Since your images are most likely different sizes, you need to make this boundary explicit. Assuming you are using a custom adapter for your gridview then setting the following within getView would probably solve your problem:

imgView.setLayoutParams(new GridView.LayoutParams(100,100));

Upvotes: 1

Related Questions