Skip to content

Instantly share code, notes, and snippets.

@eefret
Last active July 7, 2016 21:23
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 eefret/71211c1a95ffe7f71db532b6fc1b7256 to your computer and use it in GitHub Desktop.
Save eefret/71211c1a95ffe7f71db532b6fc1b7256 to your computer and use it in GitHub Desktop.
KeyboardLayoutListener Example for when you need to hide views or do something when the keyboard is up without relying in the onFocus, also to deal for when you have an item outside your scroll view and the keyboard pulls all the things up
//Import Utils from somewhere
public class BaseActivity extends Activity {
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int navigationBarHeight = Utils.getNavigationBarHeight(BaseActivity.this);
int statusBarHeight = Utils.getStatusBarHeight(BaseActivity.this);
// display window size for the app layout
Rect rect = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
// screen height - (user app height + status + nav) ..... if non-zero, then there is a soft keyboard
int keyboardHeight = rootLayout.getRootView().getHeight() - (statusBarHeight + navigationBarHeight + rect.height());
if (keyboardHeight <= 0) {
onHideKeyboard();
} else {
onShowKeyboard(keyboardHeight);
}
}
};
private boolean keyboardListenersAttached = false;
private ViewGroup rootLayout;
protected void onShowKeyboard(int keyboardHeight) {}
protected void onHideKeyboard() {}
protected void attachKeyboardListeners() {
if (keyboardListenersAttached) {
return;
}
//Getting the rootview to use it in the listener
rootLayout = (ViewGroup) findViewById(R.id.rootLayout);
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
keyboardListenersAttached = true;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (keyboardListenersAttached) {
rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(keyboardLayoutListener);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- omitted for brevity -->
</ScrollView>
<LinearLayout android:id="@+id/bottomContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- omitted for brevity -->
</LinearLayout>
</LinearLayout>
public class TestActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
//Remember to add the contentview first or it will not be able to grab the rootview
attachKeyboardListeners();
}
@Override
protected void onShowKeyboard(int keyboardHeight) {
// Here we do whatever we want when the keyboard is up
randomView.setVisibility(View.GONE);
}
@Override
protected void onHideKeyboard() {
// Here we do whatever we want when the keyboard is hidden
randomView.setVisibility(View.VISIBLE);
}
}
public class Utils {
public static int getStatusBarHeight(Context context) {
int statusBarHeightId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (statusBarHeightId > 0) {
return context.getResources().getDimensionPixelSize(statusBarHeightId);
}
return 0;
}
public static int getNavigationBarHeight( Context context) {
int navBarHeightId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (navBarHeightId > 0) {
navigationBarHeight = getResources().getDimensionPixelSize(navBarHeightId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment