0

I have a recycler view that has items with variable sizes. Now, I'm loading images using Picasso and I'd like to resize the pictures to fill the ImageView, preserving aspect ratio and by cropping the image so that it fits the ImageView perfectly. However, the width of the ImageView is 0 during the time the view holder is bound. How do I get the size?

The code looks something like this:

@Override
public void onBindViewHolder(ViewHolder holder, final int position)
{
    Foo foo = mFoos.get(position);

    // Get the size - THIS IS WHAT I WANT TO FIND OUT
    int width = ...;
    int height = 100;

    // Load the image using Picasso
    Picasso.with(mContext)
           .load(foo.getImageURL())
           .centerCrop()
           .resize(width, height)
           .into(holder.image);
}
manabreak
  • 5,415
  • 7
  • 39
  • 96

1 Answers1

0

That's because view measurement has not jet been made. So at that point width is actually 0. You need to implement ViewTreeObserver

Take a look at some of these examples:

How to get the width and height of an Image View in android?

How to get the width and height of an android.widget.ImageView?

Community
  • 1
  • 1
somerandomusername
  • 1,993
  • 4
  • 23
  • 55