Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set ImageView width and height programmatically?

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

like image 783
praveenb Avatar asked Jun 29 '10 21:06

praveenb


People also ask

How do I change the width and height of an image in Swift?

Show activity on this post. extension UIImage { func imageResize (sizeChange:CGSize)-> UIImage{ let hasAlpha = true let scale: CGFloat = 0.0 // Use scale factor of main screen UIGraphicsBeginImageContextWithOptions(sizeChange, ! hasAlpha, scale) self. draw(in: CGRect(origin: CGPoint.

How do I change the image size on Kotlin?

This example demonstrates how to resize Image in an Android App using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


2 Answers

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

imageView.getLayoutParams().height = 20; 

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

imageView.requestLayout(); 
like image 69
Hakem Zaied Avatar answered Sep 30 '22 02:09

Hakem Zaied


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); 
like image 25
InvulgoSoft Avatar answered Sep 30 '22 03:09

InvulgoSoft