4

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?

cjnash
  • 1,228
  • 3
  • 19
  • 37
  • Looks like it should work, doesn't it? Take a look at [this answer](https://stackoverflow.com/a/40028802/6287910) for another way to approach this. – Cheticamp Aug 02 '17 at 20:12
  • 1
    Since `snellen`'s parent appears to be a `ConstraintLayout`, is it possible that the constraints are so specific they're not allowing you to control the view's size? – Ben P. Aug 02 '17 at 20:52
  • @BenP. You were right. I had to set my image to just be constrained to one side of the screen instead of being constrained on all sides using my guidelines I created. Then it was able to resize the image. Just wow, I never expected that to be a problem. Feel free to post your comment as an answer – cjnash Aug 03 '17 at 12:32

2 Answers2

2

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.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
1

Try to use snellen.requestLayout() I hope that help you