views:

1384

answers:

1

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?

+3  A: 

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 H
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
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 H