Home >Java >javaTutorial >How to Programmatically Control ImageView Size at Runtime?
Controlling ImageView Dimensions Programmatically
Setting the width and height of an ImageView at runtime requires a programmatic approach. To achieve this, we can use the following steps:
Setting the Height:
// Fetch the LayoutParams, which holds the dimensions of the View LinearLayout.LayoutParams params = imageView.getLayoutParams(); // Set the height (in pixels) params.height = 20; // This step is crucial! After modifying the layout params, request a layout pass to refresh the view imageView.requestLayout();
Important Note:
If you're modifying the ImageView's height after the layout has been drawn, using requestLayout() is essential to ensure that the changes are applied correctly.
Setting the Width:
Follow the same steps as above, but use the width property of the LinearLayout.LayoutParams object.
params.width = 40;
The above is the detailed content of How to Programmatically Control ImageView Size at Runtime?. For more information, please follow other related articles on the PHP Chinese website!