80

I want to set the LayoutParams for an ImageView but cant seem to find out the proper way to do it.

I can only find documentation in the API for the various ViewGroups, but not an ImageView. Yet the ImageView seems to have this functionality.

This code doesn't work...

myImageView.setLayoutParams(new ImageView.LayoutParams(30,30));

How do I do it?

Renjith
  • 5,783
  • 9
  • 31
  • 42
DeadTime
  • 841
  • 1
  • 6
  • 6

5 Answers5

169

You need to set the LayoutParams of the ViewGroup the ImageView is sitting in. For example if your ImageView is inside a LinearLayout, then you create a

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

This is because it's the parent of the View that needs to know what size to allocate to the View.

Steve Haley
  • 55,374
  • 17
  • 77
  • 85
  • 3
    You know... what you said worked. But I had grasped that concept already but was making a big mistake. You see I had my ImageViews in a TableLayout... so I was using TableLayout.setLayoutParams. But it would crash. When I thought deeper about it, I needed to drill down to TableRow.setLayoutParams for it to finally work. Thanks for making my brain work. the 'is sitting in' triggered it for me... gotta dumb this stuff down. – DeadTime Jun 03 '10 at 23:11
  • 22
    I'm glad I could help. If your problem is solved, could you mark this as the accepted answer? It's the tickmark to the left of the text. – Steve Haley Jun 04 '10 at 11:57
  • Hi @SteveHaley, the width and height parameters here are in pixel right? My experience shows that and it's shockingly blank here: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#LinearLayout.LayoutParams(int, int) – ericn Aug 23 '13 at 08:09
  • 2
    @fuzzybee I think it's in pixels... Most layout stuff set in code is pixels rather than DP. Convert pixels to DP by using something like `TypedValue.applyDimension(TypedValue.ComplexUnit_DP, 30, getResources().getDisplayMetrics())` – Steve Haley Aug 23 '13 at 15:48
19

Old thread but I had the same problem now. If anyone encounters this he'll probably find this answer:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);

This will work only if you add the ImageView as a subView to a LinearLayout. If you add it to a RelativeLayout you will need to call:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);
Bogdan
  • 191
  • 1
  • 2
13

If you're changing the layout of an existing ImageView, you should be able to simply get the current LayoutParams, change the width/height, and set it back:

android.view.ViewGroup.LayoutParams layoutParams = myImageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
myImageView.setLayoutParams(layoutParams);

I don't know if that's your goal, but if it is, this is probably the easiest solution.

OrangeCMS
  • 55
  • 4
Karl Giesing
  • 1,654
  • 3
  • 16
  • 24
  • Is it necessary to assign again the LayoutParams with `myImageView.setLayoutParams(layoutParams);` ? Since you modify the reference to the LayoutParams it should not be. – Vince Jun 25 '18 at 14:33
6

An ImageView gets setLayoutParams from View which uses ViewGroup.LayoutParams. If you use that, it will crash in most cases so you should use getLayoutParams() which is in View.class. This will inherit the parent View of the ImageView and will work always. You can confirm this here: ImageView extends view

Assuming you have an ImageView defined as 'image_view' and the width/height int defined as 'thumb_size'

The best way to do this:

ViewGroup.LayoutParams iv_params_b = image_view.getLayoutParams();
iv_params_b.height = thumb_size;
iv_params_b.width = thumb_size;
image_view.setLayoutParams(iv_params_b);
Codeversed
  • 9,287
  • 3
  • 43
  • 42
0

In order not to lose the rest of the parameters, you need to do as shown below:

Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.checked, null);    
preview.setImageDrawable(drawable);
ViewGroup.LayoutParams layoutParams = preview.getLayoutParams();
layoutParams.width = 100;
layoutParams.height = 98;
preview.setLayoutParams(layoutParams);
Mawon
  • 71
  • 1
  • 6