Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you setLayoutParams() for an ImageView?

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?

like image 367
DeadTime Avatar asked Jun 03 '10 11:06

DeadTime


People also ask

What is layout params?

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports. The base LayoutParams class just describes how big the view wants to be for both width and height.

What is Linearlayout LayoutParams?

This set of layout parameters defaults the width and the height of the children to ViewGroup. LayoutParams. WRAP_CONTENT when they are not specified in the XML file. TableLayout.LayoutParams. This set of layout parameters enforces the width of each child to be ViewGroup.


2 Answers

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.

like image 75
Steve Haley Avatar answered Oct 05 '22 03:10

Steve Haley


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); 
like image 41
Bogdan Avatar answered Oct 05 '22 03:10

Bogdan