Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@oscarito9410
Last active October 12, 2020 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarito9410/b58bbbf6aef0b27432a6da56ca249fc9 to your computer and use it in GitHub Desktop.
Save oscarito9410/b58bbbf6aef0b27432a6da56ca249fc9 to your computer and use it in GitHub Desktop.
/**
* An OnGlobalLayoutListener interface that will adjust and resize a fullscreen scene's view when the soft keyboard is shown
* or hidden.
* <p>
* Provides custom layout behavior to the 'adjustResize' windowSoftInputMode for a View layout by enforcing an adjustable
* container with the option of a static footer view. The adjustable container is "adjusted" by increasing or decreasing the
* view's translationY property equal to the distance of the adjustable view that was obscured by the soft keyboard.
* <p>
* When adhering to this interface, be sure to set the relevant Activity's windowSoftInputMode to 'adjustResize' so that the
* onGlobalLayout method is called when the keyboard is shown and dismissed.
* <p>
* Created by Ryan Taylor on 2/2/16.
*/
public class AdjustingViewGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private View mAdjustableView;
private int mOldVisibleFrameBottom;
private int mVisibleWindowFrameBottom;
private final int mSoftKeyboardEstimatedMinHeight;
public AdjustingViewGlobalLayoutListener (View adjustableView) {
mAdjustableView = adjustableView;
Rect visibleWindowFrame = new Rect();
mAdjustableView.getWindowVisibleDisplayFrame(visibleWindowFrame);
mVisibleWindowFrameBottom = visibleWindowFrame.bottom;
mSoftKeyboardEstimatedMinHeight = mVisibleWindowFrameBottom / 4;
}
@Override
public void onGlobalLayout () {
Rect visibleWindowRect = new Rect();
mAdjustableView.getWindowVisibleDisplayFrame(visibleWindowRect);
int[] adjustableViewCoords = {0, 0};
mAdjustableView.getLocationOnScreen(adjustableViewCoords);
int adjustableViewBottom = adjustableViewCoords[1] + mAdjustableView.getHeight();
int deltaVisibleWindowBottom = mOldVisibleFrameBottom - visibleWindowRect.bottom;
ObjectAnimator translationYAnimator = null;
if (Math.abs(deltaVisibleWindowBottom) > mSoftKeyboardEstimatedMinHeight) {
// The visible height of the window has changed significantly enough
// to suggest that the soft keyboard has been shown or hidden.
boolean softKeyboardVisible = deltaVisibleWindowBottom > 0;
// Subtract the height of the footer from the height of the soft keyboard to get the adjustment length
int adjustmentLength = deltaVisibleWindowBottom - (mVisibleWindowFrameBottom - adjustableViewBottom);
if (softKeyboardVisible) {
translationYAnimator = ObjectAnimator.ofFloat(mAdjustableView, Constants.AppExtras.TRANSLATIONY, -adjustmentLength);
} else if (Math.abs(deltaVisibleWindowBottom)
> ((ViewGroup.MarginLayoutParams) mAdjustableView.getLayoutParams()).bottomMargin) {
// The soft keyboard has been hidden.
translationYAnimator = ObjectAnimator.ofFloat(mAdjustableView, Constants.AppExtras.TRANSLATIONY, 0);
}
} else if (mVisibleWindowFrameBottom - visibleWindowRect.bottom > mSoftKeyboardEstimatedMinHeight
&& deltaVisibleWindowBottom != 0) {
// The soft keyboard is most likely still visible, and an attachment, such as the text suggestion bar,
// was added to the soft keyboard.
translationYAnimator = ObjectAnimator.ofFloat(mAdjustableView, Constants.AppExtras.TRANSLATIONY, -deltaVisibleWindowBottom
+ mAdjustableView.getTranslationY());
}
if (translationYAnimator != null) {
// Translate the mAdjustableContainer up or down equal to the distance of the height of the soft keyboard.
translationYAnimator.start();
}
mOldVisibleFrameBottom = visibleWindowRect.bottom;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment