0

I'm attempting to change the size of my activity in OnAttachedToWindow, so that it appears floating. In order to calculate it's new size, I need to get the visible display frame for the decor view. But it returns 0.

I actually need to get the height of the status bar. I followed this guide: How to get Android screen height minus status bar in pixel?

But it doesn't work.

How can I get the height of the status bar (or the visible display frame) in OnAttachedToWindow?

I would have thought this possible since, apparently, it has been attached, and drawing can commence, according to the Android docs.

Here is my Activity code:

public override void OnAttachedToWindow()
{
    base.OnAttachedToWindow();

    var decorView = Window.DecorView;
    var rect = new Rect();

    decorView.GetWindowVisibleDisplayFrame(rect);

    // rect is now 0

    var lp = (WindowManagerLayoutParams)decorView.LayoutParameters;

    // change some stuff...

    WindowManager.UpdateViewLayout(decorView, lp);
}
HelloWorld
  • 3,381
  • 5
  • 32
  • 58

1 Answers1

0

The docs actually state this about OnAttachedToWindow():

...it may be called any time before the first onDraw -- including before or after onMeasure(int, int).

That's why I wouldn't really rely on OnAttachedToWindow()/OnDetachedToWindow() being in sync with neither OnMeasure() or OnLayout().

Is it an idea to override OnSizeChanged() method to know when it's measured and apply what you want in there?

Reference: OnSizeChanged(int w, int h, int oldw, int oldh)

Raimo
  • 1,494
  • 1
  • 10
  • 19