-
-
Save kaierwen/bb4aa582e8ea5a8f92900e1b2799cb4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SoftInputAssist { | |
| private View rootView; | |
| private ViewGroup contentContainer; | |
| private ViewTreeObserver viewTreeObserver; | |
| private ViewTreeObserver.OnGlobalLayoutListener listener = () -> possiblyResizeChildOfContent(); | |
| private Rect contentAreaOfWindowBounds = new Rect(); | |
| private FrameLayout.LayoutParams rootViewLayout; | |
| private int usableHeightPrevious = 0; | |
| public SoftInputAssist(Activity activity) { | |
| contentContainer = (ViewGroup) activity.findViewById(android.R.id.content); | |
| rootView = contentContainer.getChildAt(0); | |
| rootViewLayout = (FrameLayout.LayoutParams) rootView.getLayoutParams(); | |
| } | |
| public void onPause() { | |
| if (viewTreeObserver.isAlive()) { | |
| viewTreeObserver.removeOnGlobalLayoutListener(listener); | |
| } | |
| } | |
| public void onResume() { | |
| if (viewTreeObserver == null || !viewTreeObserver.isAlive()) { | |
| viewTreeObserver = rootView.getViewTreeObserver(); | |
| } | |
| viewTreeObserver.addOnGlobalLayoutListener(listener); | |
| } | |
| public void onDestroy() { | |
| rootView = null; | |
| contentContainer = null; | |
| viewTreeObserver = null; | |
| } | |
| private void possiblyResizeChildOfContent() { | |
| contentContainer.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds); | |
| int usableHeightNow = contentAreaOfWindowBounds.height(); | |
| if (usableHeightNow != usableHeightPrevious) { | |
| rootViewLayout.height = usableHeightNow; | |
| rootView.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom); | |
| rootView.requestLayout(); | |
| usableHeightPrevious = usableHeightNow; | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment