Screen rotation, makes a view to be 0 sized

107 Views Asked by At

I have a AppCompatDialogFragment with this code:

    View anchorView = getActivity().findViewById("R.id.myView")

 if (ViewCompat.isLaidOut(anchorView)) {
      anchorView.getLocationInWindow(location);
      Log.w(
                      "*****",
          "====alignWithAnchorView:: anchor width="
              + anchorView.getWidth()
              + " anchor x="
              + location[0]
              + " anchor y="
              + location[1]);
      alignWithAnchorView(anchorView);

    } else {
      anchorView
          .getViewTreeObserver()
          .addOnGlobalLayoutListener(
              new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                  anchorView.getLocationInWindow(location);
                  Log.w(
                      "*****",
                      "====onGlobalLayout:: anchor width="
                          + anchorView.getWidth()
                          + " anchor x="
                          + location[0]
                          + " anchor y="
                          + location[1]);
                  alignWithAnchorView(anchorView);
                  OneGoogleViewCompat.removeOnGlobalLayoutListener(anchorView, this);
                }
              });
    }

The first time I call this, I get the view positioned and inflated as it's already laidOut.

But when I change to landscape I get the anchorView is positioned (0,0) and width ==0

What can cause this on-configuration change? as the view is found by id, and I wait for onGlobalLayout

I do another rotation and the size is still 0.

I can see the view after all rotation (there is a dimming screen but it's there even before the first rotation)

before & after it looks the same:

enter image description here

0

There are 0 best solutions below