What I have:
- An activity with
android:configChanges="orientation|screenSize|keyboardHidden" - A DialogFragment on it
- ViewA on DialogFragment
What's the problem:
I'm using ViewA.getLocationOnScreen to get the location on the screen of the view. when I first open the dialog the position is correct. After I rotate the screen, because of android:configChanges the view somehow doesn't update it's position and even if the dialog is correctly centered in the activity the getLocationOnScreen of ViewA points to the older location, before the rotation.
What I've tried.
I overwrote the onConfigurationChanged of the dialog and tried this:
ViewA.requestLayout(doesn't do anything)ViewA.getViewTreeObserver().addOnGlobalLayoutListenerand on the onGlobalLayout set the topMargin to 1 and call requestLayout again. (this worked but I don't want to set the margin every time I rotate the screen)
What I want to know is how can I force the reposition of the dialog, making getLocationOnScreen return the correct values after a rotation
Note that I don't want to change android:configChanges
The view's location on screen has not yet been updated when
onConfigurationChangedis called. You need to add anOnLayoutChangeListenerto the view to catch the updates you're looking for. See the example below.TestDialogFragment.java
test_fragment.xml
Log Output