ab11
ab11

Reputation: 20090

In Android, how to re-layout views?

I am adding a series of custom views to a LinearLayout. I have overridden the onMeasure method of these custom views, to return the dimensions based on certain parameters. Based on user input, I would like to change these parameters, to change the size of the views. How can I force the LinearLayout to "re-layout" it's children, to reflect the new dimensions?

Upvotes: 18

Views: 17282

Answers (2)

Helin Wang
Helin Wang

Reputation: 4202

my case is I need to to relayout all children immediately. requestLayout() does not do it immediately.

For those who have the same requirement with me:

This will force relayout the view's children immediately (given that view's own width and height does not need to change)

private void reLayoutChildren(View view) {
    view.measure(
        View.MeasureSpec.makeMeasureSpec(view.getMeasuredWidth(), View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(view.getMeasuredHeight(), View.MeasureSpec.EXACTLY));
    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}

Upvotes: 14

ab11
ab11

Reputation: 20090

Oh... linearLayout.requestLayout().

Upvotes: 26

Related Questions