Explain Codes LogoExplain Codes Logo

Set ImageView width and height programmatically?

java
imageview
layoutparams
responsive-design
Anton ShumikhinbyAnton Shumikhin·Mar 6, 2025
TLDR

Changing the dimensions of an ImageView in Android programmatically is a breeze with the LayoutParams tool. All you need to do is set up a new LayoutParams with the desired width and height, and assign it to your ImageView.

Here's a quick look at how to do it:

For LinearLayout:

ImageView imageView = (ImageView) findViewById(R.id.imageView); // width and height as quick as a ninja! LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 200); imageView.setLayoutParams(layoutParams);

For RelativeLayout:

ImageView imageView = (ImageView) findViewById(R.id.imageView); // let the magic begin! RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, // width RelativeLayout.LayoutParams.MATCH_PARENT); // height imageView.setLayoutParams(layoutParams);

Change pixel values as required or use WRAP_CONTENT and MATCH_PARENT to fit the ImageView neatly within its parent.

Debugging dynamic Initialization and Layouts

Working with dynamic image views or varying layouts? Here are some handy instructions:

  • Dynamic ImageView creation:
// Say hi to your new ImageView! ImageView imageView = new ImageView(context); ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); imageView.setLayoutParams(layoutParams); // Welcome, imageView, to your new home! parentLayout.addView(imageView);
  • Responsive to the screen size: Let's convert dp units to pixels, using TypedValue and DisplayMetrics:
// Listen to the wisdom of elders, 1dp != 1px int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpWidth, getResources().getDisplayMetrics()); imageView.getLayoutParams().width = width; imageView.requestLayout(); // Poke ImageView to update layout

Dodge the exceptions and optimize

Fear that haunting ClassCastException? An accurate case of LayoutParams can help:

// Hang on old Params! No need to abandon yet. ViewGroup.LayoutParams params = imageView.getLayoutParams(); params.width = 300; // Replace 300 with your width, your wish my command! params.height = 150; // 150 soaring to desired height imageView.setLayoutParams(params); imageView.requestLayout(); // Update progress 100%

Responsive sizing, the smart way

  • Matching parent size: Got an ImageView that needs to take up all available space? Use MATCH_PARENT:
ViewGroup.LayoutParams params = imageView.getLayoutParams(); params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT; imageView.setLayoutParams(params); // Congratulations, you're omnipresent now, ImageView!
  • No stretch, just fit: Want the ImageView to just cover its content without stretching? Here's the trick:
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); ViewGroup.LayoutParams params = imageView.getLayoutParams(); params.width = ViewGroup.LayoutParams.WRAP_CONTENT; params.height = ViewGroup.LayoutParams.WRAP_CONTENT; imageView.setLayoutParams(params); // Like your favourite jeans, fits just right!