Skip to content

Commit

Permalink
Prevent keyboard from closing immediately after opening.
Browse files Browse the repository at this point in the history
  • Loading branch information
cody-signal committed Feb 1, 2023
1 parent 56354f6 commit c76ca95
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
* LinearLayout that, when a view container, will report back when it thinks a soft keyboard
Expand All @@ -45,6 +46,8 @@
public class KeyboardAwareLinearLayout extends LinearLayoutCompat {
private static final String TAG = Log.tag(KeyboardAwareLinearLayout.class);

private static final long KEYBOARD_DEBOUNCE = 150;

private final Rect rect = new Rect();
private final Set<OnKeyboardHiddenListener> hiddenListeners = new HashSet<>();
private final Set<OnKeyboardShownListener> shownListeners = new HashSet<>();
Expand All @@ -63,6 +66,7 @@ public class KeyboardAwareLinearLayout extends LinearLayoutCompat {
private boolean keyboardOpen = false;
private int rotation = 0;
private boolean isBubble = false;
private long openedAt = 0;

public KeyboardAwareLinearLayout(Context context) {
this(context, null);
Expand Down Expand Up @@ -183,13 +187,21 @@ private int getAvailableHeight() {
protected void onKeyboardOpen(int keyboardHeight) {
Log.i(TAG, "onKeyboardOpen(" + keyboardHeight + ")");
keyboardOpen = true;
openedAt = System.currentTimeMillis();

notifyShownListeners();
}

protected void onKeyboardClose() {
if (System.currentTimeMillis() - openedAt < KEYBOARD_DEBOUNCE) {
Log.i(TAG, "Delaying onKeyboardClose()");
postDelayed(this::updateKeyboardState, KEYBOARD_DEBOUNCE);
return;
}

Log.i(TAG, "onKeyboardClose()");
keyboardOpen = false;
openedAt = 0;
notifyHiddenListeners();
}

Expand Down

0 comments on commit c76ca95

Please sign in to comment.