0

I am using the FAB from the new design support library by Google. I have screen with a long form and a FAB. I want the FAB disappear when soft keyboard opens. Cannot find a way to detect soft keyboard open. Is there any other option

I cannot set a listener to EditText as the are all contained in a different Fragments and the on focus change listener is not available in another Fragments.

I have implemented FAB in main Activity so I couldn't do hide key board listener for EditText focus listener can anyone please have a solution share with me.

manfcas
  • 1,933
  • 7
  • 28
  • 47
prasanth.s
  • 27
  • 2
  • 9

2 Answers2

3

There is no direct way to know when the soft keyboard opens, however you can do the following:

contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();

    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    int keypadHeight = screenHeight - r.bottom;

    if (keypadHeight > screenHeight * 0.15) {
        // keyboard is opened
        // Hide your FAB here
    }
    else {
        // keyboard is closed
    }
}
});
zed
  • 3,180
  • 3
  • 27
  • 38
  • Using view pager i implemented mutilple fragments how to use this code can u plz tell me. i tried but fab was opened and closed suddenly then fab was in mainactivity it shows all the fragments – prasanth.s Feb 15 '17 at 10:39
0

You could listen for the opening and closing of the keyboard.

public class BaseActivity extends Activity {
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = rootLayout.getRootView().getHeight() - rootLayout.getHeight();
        int contentViewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(BaseActivity.this);

        if(heightDiff <= contentViewTop){
            onHideKeyboard();

            Intent intent = new Intent("KeyboardWillHide");
            broadcastManager.sendBroadcast(intent);
        } else {
            int keyboardHeight = heightDiff - contentViewTop;
            onShowKeyboard(keyboardHeight);

            Intent intent = new Intent("KeyboardWillShow");
            intent.putExtra("KeyboardHeight", keyboardHeight);
            broadcastManager.sendBroadcast(intent);
        }
    }
};

private boolean keyboardListenersAttached = false;
private ViewGroup rootLayout;

protected void onShowKeyboard(int keyboardHeight) {}
protected void onHideKeyboard() {}

protected void attachKeyboardListeners() {
    if (keyboardListenersAttached) {
        return;
    }

    rootLayout = (ViewGroup) findViewById(R.id.rootLayout);
    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);

    keyboardListenersAttached = true;
}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (keyboardListenersAttached) {
        rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(keyboardLayoutListener);
    }
}
}

More detailed example listed in this question: SoftKeyboard open and close listener in an activity in Android?

Community
  • 1
  • 1
Stefan
  • 2,098
  • 2
  • 18
  • 29