0

I have an adapter to a ListView is a list of ImageViews. I am using a stretch to make the image fil the imageview so I can take smaller images and make them larger on the screen, however the ImageView normally just uses wrap_content and this is an issue because the images just show up as their normal width and height. Is there any way I can set the height and width of a view before drawing it because as in this case I do not have control over the view after it has been drawn. Here is my aapter method:

  @Override
public View getView(int position, View convertView, ViewGroup parent) {
    String currentImage  = getItem(position);
    ScaleType scaleType = ScaleType.FIT_CENTER;
    float screenWidth = parent.getResources().getDisplayMetrics().widthPixels;

    if (convertView == null) {
        convertView = new ImageView(parent.getContext()); 
    }
  //         WHAT I WOULD LIKE TO BE ABLE TO DO, but this returns null pointer exception
 //     convertView.getLayoutParams().width = (int) screenWidth;
//      convertView.getLayoutParams().height = (int) ((float)(screenWidth/currentImage.getWidth())*currentImage.getHeight());

    ((ImageView) convertView).setScaleType(scaleType);
    ((ImageView) convertView).setImageBitmap(MainActivity.cache.getBitmap(currentImage));
    return convertView;
}
mmBs
  • 8,421
  • 6
  • 38
  • 46
Osman
  • 1,771
  • 4
  • 24
  • 47

1 Answers1

0

How about something like someView.setHeight() and someView.setWidth()? Or someView.setLayoutParams()? You could add either of these to the overridden getView() callback and it should take care of your problem.

You could also Create a Custom View and override something like getMeasuredWidthAndState(). (I think that's one of the right methods, but I'm not one hundred percent sure.) You could create a width class variable and a height class variable that all instances of your custom ImageView would use. However, that might be a bit much if you just want to set the layout width and height though.

Don
  • 506
  • 2
  • 8
  • 23