michael
michael

Reputation: 110510

Why getHeight() of a Viewgroup does not change

I have a LinearLayout (ViewGroup). And in my code, I have added a child view (another linear layout) to the LinearLayout. My question is why the LinearLayout 's getHeight() return the SAME value? On the phone, I do see my child linear layout is visible and is displayed correctly. But why the getHeight() does not return me the right value?

And I have looked at the source code of add() in ViewGroup. It does call 'requestLayout()' so I expect the getHeight() of the ViewGroup does get update correctly. Am I correct?

Upvotes: 1

Views: 400

Answers (1)

Romain Guy
Romain Guy

Reputation: 98501

getHeight() is not updated immediately, requestLayout() requests a layout pass that happens later (it's asynchronous). You must post an event in the UI events queue to retrieve the height after the layotu pass:

requestLayout();
post(new Runnable() {
  public void run() {
    // getHeight
  }
});

Upvotes: 4

Related Questions