Search code examples
androidimageviewlayoutparams

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


I have done this in the past and I am frustrated because this method is not working like I expect it to. I have an image that I want to resize to specific dimensions and I am using the code below:

snellen.post(new Runnable() {
        @Override
        public void run() {
            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) snellen.getLayoutParams();
            params.width = (int)(converted*WIDTH_RATIO);
            params.height = (int)(converted*HEIGHT_RATIO);

            snellen.setLayoutParams(params);

            System.out.println("########################################################################### SNELLEN CHART WIDTH: : " + snellen.getWidth());
            System.out.println("########################################################################### SNELLEN CHART HEIGHT: : " + snellen.getHeight());
        }
    });

I have checked what both (converted*WIDTH_RATIO) and (converted*HEIGHT_RATIO) are and they are somewhere in the 100's. I have put it inside a handler so I can get the correct getLayoutParams() because I have had trouble with that in the past.

The problem is that when I print out what the width and hights are they are not what I have set them to inside the params.width and params.height. What am I doing wrong here?


Solution

  • 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.