idish idish - 1 year ago 184
Android Question

Android ImageView setting Bitmap FitXY doesn't work

I've been trying for really long time to set the bitmap so it will fit the imageView bounds.
It just doesn't work via scaletype: fitXY. My bitmap images size are lower than the ImageView's (Saved on 100X100 pixels), I want my bitmap images to fit and strech into the imageView.

Here's some code:

<ImageView xmlns:android=""

Here's the code of setting the imageView. (I'm seeting the scaleType in different places for testing..)

public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;

imageLoader.displayImage(imagesIds[position], imageView, options);
return imageView;

Here's how it looks:

enter image description here

As you can see, the Height of the imageView is fine, but I want the Width of it to be the same. I do not have access for my bitmap image via my activity, so please don't ask me to resize my bitmap. I really don't understand why it just doesn't work as it should.

Thank you!

EDIT: Just to make the problem more understandable, The imageview height is calculated for each device. It is different for each device. I want the width of my imageview, to be the same as the height as it is. I am using the Two-WayGridView library to show the images if it makes any difference.

Answer Source

Using scaleType will not change the size of your ImageView, as you seem to think it should. All it does is scale the bitmap up to the size of the ImageView(which it is doing quite well, judging by your screenshot).

If you want to make square ImageViews, you should try looking at this question.

Also, I'm not sure I understand why you're using the TwoWayGridView library. It's specifically designed to allow you to state the number of rows/columns, and it will stretch the items to fit. This seems to be the exact opposite of what you want, since you want to make them square. A normal GridView will work, provided you override the onMeasure() of each item like it shows in the linked quesion/answer.