Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active May 3, 2018 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mitchtabian/c9f515b5c9b27e60ccfa1f9f9e9720fb to your computer and use it in GitHub Desktop.
Save mitchtabian/c9f515b5c9b27e60ccfa1f9f9e9720fb to your computer and use it in GitHub Desktop.
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