Changing LayoutParams on an Android view has no effect, even after calling requestLayout

Viewed 2000

I've run into this situation two or three times, where I have some code that looks like this:

  ViewGroup.LayoutParams params = myView.getLayoutParams();
  params.height = 240;
  myView.requestLayout();

Or alternatively:

  ViewGroup.LayoutParams params = myView.getLayoutParams();
  params.height = 240;
  myView.setLayoutParams(params);

And the view size never changes. I've tried the following, with no luck:

  1. Calling forceLayout, which ironically seems less forceful than requestLayout, because it doesn't notify the View's parents that a layout is needed.
  2. Overriding onMeasure() to see if it is ever called (it isn't).
  3. Making sure that I'm not making the call to requestLayout during a Layout pass.

I've also tried calling requestLayout on all of the View's parents, like so:

  ViewParent parent = myView.getParent();
  while (parent != null) {
      parent.requestLayout();
      parent = parent.getParent();
  }

This works, but it seems really hacky. I'd much rather find the real solution.

What am I doing wrong?

1 Answers
Related