Skip to content

Instantly share code, notes, and snippets.

@sergenes
Created March 9, 2016 07:52
Show Gist options
  • Save sergenes/9ebee9d91c75b5cde7d7 to your computer and use it in GitHub Desktop.
Save sergenes/9ebee9d91c75b5cde7d7 to your computer and use it in GitHub Desktop.
Android - Fragment - show/hide keyboard event and move up the main layout
Rect visibleRect = new Rect();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup mMain = (ViewGroup) inflater.inflate(R.layout.select_cover_album, container, false);
mMain.getWindowVisibleDisplayFrame(visibleRect);
mMain.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
Rect r = new Rect();
mMain.getWindowVisibleDisplayFrame(r);
if(r.height() < visibleRect.height()){
mMain.postDelayed(new Runnable() {
@Override
public void run() {
mMain.setY(-70);
}
}, 300);
}
if(r.height() == visibleRect.height() && mMain.getY() < 0){
mMain.setY(0);
}
}
});
return mMain;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment