Last active
September 14, 2022 21:45
-
-
Save calvince/18239d6a176dd9ab0936b0de4c32b4e8 to your computer and use it in GitHub Desktop.
A listener for the soft keyboard on the screen
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
private IMainActivity mIMainActivity; | |
static int mAppHeight; | |
static int currentOrientation = -1; | |
public void setKeyboardVisibilityListener() { | |
final View contentView = getActivity().findViewById(android.R.id.content); | |
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | |
private int mPreviousHeight; | |
@Override | |
public void onGlobalLayout() { | |
int newHeight = contentView.getHeight(); | |
if (newHeight == mPreviousHeight) | |
return; | |
mPreviousHeight = newHeight; | |
Log.d(TAG, "onGlobalLayout: new height: " + newHeight); | |
if (getActivity().getResources().getConfiguration().orientation != currentOrientation) { | |
currentOrientation = getActivity().getResources().getConfiguration().orientation; | |
mAppHeight =0; | |
Log.d(TAG, "onGlobalLayout: current Orientation: " + currentOrientation); | |
Log.d(TAG, "onGlobalLayout: app height: " + mAppHeight); | |
} | |
if (newHeight >= mAppHeight) { | |
mAppHeight = newHeight; | |
Log.d(TAG, "onGlobalLayout: app height: " + mAppHeight); | |
} | |
Log.d(TAG, "onGlobalLayout: -------------------------\n"); | |
if (newHeight != 0) { | |
MessagesFragment messagesFragment = (MessagesFragment) getActivity() | |
.getSupportFragmentManager().findFragmentByTag(getActivity().getString(R.string.tag_fragment_messages)); | |
if(messagesFragment.isVisible()){ | |
if (mAppHeight > newHeight) { | |
Log.d(TAG, "onGlobalLayout: hiding bottom nav"); | |
// Height decreased: keyboard was shown | |
mIMainActivity.setBottomNavigationVisibility(false); | |
} | |
else { | |
Log.d(TAG, "onGlobalLayout: showing bottom nav"); | |
// Height increased: keyboard was hidden | |
mIMainActivity.setBottomNavigationVisibility(true); | |
} | |
} | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment