Set ImageView width and height programmatically?


Question

How can I set an ImageView's width and height programmatically?

1
599
1/31/2013 1:25:05 AM

Accepted Answer

It may be too late but for the sake of others who have the same problem, to set the height of the ImageView:

image_view.getLayoutParams().height = 20;

Hope this helps.

Important. If you're setting the height after the layout has already been 'laid out', make sure you also call:

image_view.requestLayout()
1015
3/30/2015 6:49:30 PM

If your image view is dynamic, the answer containing getLayout will fail with null-exception.

In that case the correct way is:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
iv.setLayoutParams(layoutParams);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon