Set ImageView width and height programmatically?

To set the width and height of an ImageView programmatically, you can use the setLayoutParams() method and pass it a LayoutParams object. The LayoutParams object defines the width and height of the view, as well as other layout parameters such as margins and gravity.

Here is an example of how you can use the setLayoutParams() method to set the width and height of an ImageView in Java:

ImageView imageView = new ImageView(this);

// set the width and height of the ImageView to 200 pixels
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(200, 200);
imageView.setLayoutParams(layoutParams);

Alternatively, you can use the setMaxWidth() and setMaxHeight() methods to set the maximum width and height of the ImageView:

ImageView imageView = new ImageView(this);

// set the maximum width and height of the ImageView to 200 pixels
imageView.setMaxWidth(200);
imageView.setMaxHeight(200);

Keep in mind that the width and height of the ImageView will be determined by the layout manager of the parent view. If the parent view has a fixed size, the ImageView will be constrained to that size. If the parent view has wrap_content as the width or height, the ImageView will be sized to fit its content.