Reputation: 1387
I have an Activity which handles orientation change and I want to manually resize a layout when the device is rotated. In the onLayout of that layout, I call setLayoutParams:
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (changed) {
int orientation = getResources().getConfiguration().orientation;
// Only resize on actual orientation change
if (orientation != mLastOrientation) {
// Apply the new layout size
setLayoutParams(new LinearLayout.LayoutParams(someNewWidth, someNewHeight);
mLastOrientation = orientation;
}
}
}
This works fine on my newer 4.x devices but on the 2.x devices, the setLayoutParams appears to run one orientation "late".
So on the first rotation from portrait to landscape, the resize does not occur, then on subsequent rotations it shows my landscape size on portrait, portrait size on landscape, and so on.
I read in the setLayoutParams source that it calls requestLayout which is supposed to redraw the layout, but doesn't appear to be doing this immediately. I have also tried invalidate() which does not work either.
Any ideas on why setLayoutParams is not applying on the first rotation, or alternative solutions?
Upvotes: 0
Views: 194
Reputation: 183
Altering the LayoutParams
during the layout causes the multiple layout passes to be done and can cause serious slowdown if when displaying complex layouts.
It is much more efficient to override the onMeasure()
method of your view and set the measured size there. Take a look at this answer https://stackoverflow.com/a/12267248/3811368 for an example of this technique.
Upvotes: 1