How to Setlayoutparams() for an Imageview

How to set LayoutParams for ImageView

You need to specify which LayoutParams you need to use, it must be
based on parent layout which can be anything
LinearLayout.LayoutParams or RelativeLayout.LayoutParams or FrameLayout.LayoutParams.

RelativeLayout imageLayout = new RelativeLayout(this);

ImageView imageview = new ImageView(this);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setImageResource(R.drawable.nature);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

imageLayout.addView(iv, lp);

or

imageview.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
imageLayout.addView(iv);

Why isn't setLayoutParams changing the size of my ImageView?

Since you're casting snellen's LayoutParams to ConstraintLayout.LayoutParams, it seems likely that your view is the child of a ConstraintLayout.

If that is the case, it is possible that the problem is that your view's dimensions are defined by its set of constraints, and so changing params.width and params.height will have no effect.

Check your view's constraints and make sure they are not determining the view's dimensions.

Set other layout parameters imageview

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
lp.bottomMargin = ...
lp.leftMargin = ...
lp.rightMargin = ...
lp.topMargin = ...

imageView.setLayoutParams(lp);

setLayoutParams resets ImageView position

Perhaps you should use it this way:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.width = (int) (200 * scale + 0.5f);
layoutParams.height = (int) (250 * scale + 0.5f);
imageView.setLayoutParams(layoutParams); //I think this part might not be needed.

changing height and width of imageview in android

UPDATE:

Do this:

int height=imageView.getHeight();
int width=imageView.getWidth();
imageView.setLayoutParams(new LinearLayout.LayoutParams(height, width));


Suggesting your ImageView is located inside a LinearLayout you need to do this:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
params.setHeight(XX);
params.setWidth(XX);
imageView.setLayoutParams(params);

I don't know if the syntax is perfectly correct, but basically this is how you do it, don't forget that you need to update your UI, in order to see any changes.



Related Topics



Leave a reply



Submit