I'm using this library: https://github.com/yshrsmz/KeyboardVisibilityEvent to detect when the keyboard is opened or closed and this relies on android:windowSoftInputMode="adjustResize"
being input to the Android manifest.
This library works perfectly to detect the opened and closed events of the soft keyboard but my content is pushed out of view because of the adjustResize parameter.
Java:
KeyboardVisibilityEvent.setEventListener( AddActivity.this, new KeyboardVisibilityEventListener() { @Override public void onVisibilityChanged(boolean isOpen) { // some code depending on keyboard visiblity status if (noteEditText.isFocused()) { if (isOpen) { Log.d("KB", "Keyboard is open"); noteEditText.setLines(12); noteEditText.setCursorVisible(true); } else { Log.d("KB", "Keyboard is closed"); noteEditText.setLines(50); noteEditText.setCursorVisible(false); } } } }); noteEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { Log.d("KB", "onFocusChange"); if (firstStart) { noteEditText.setLines(12); noteEditText.setCursorVisible(true); firstStart = false; } } });
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/add_record" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" android:windowSoftInputMode="stateHidden"> <EditText android:id="@+id/title_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/enter_title" android:inputType="textCapSentences" android:textColor="@color/fontPrimary" android:theme="@style/EditTextCustomCursor"> <requestFocus /> </EditText> <EditText android:id="@+id/note_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:ellipsize="end" android:gravity="top|left" android:hint="@string/enter_note" android:inputType="textCapSentences|textMultiLine" android:lines="50" android:maxLines="20" android:minLines="5" android:paddingLeft="5dp" android:scrollHorizontally="false" android:scrollbars="vertical" android:textColor="@color/fontPrimary" android:theme="@style/EditTextCustomCursor" />
So this works great by adjusting the lines of the second EditText so then I'm typing above the keyboard but when I close the keyboard, scroll to the bottom of that EditText and click at the bottom, the EditText cursor is placed where I clicked but then it pushes the first EditText and the Support ActionBar out of view and leaves a large gap at the bottom (as can be seen in the image (image 2) below, as where the 'F' is selected, that is the bottom of the EditText).
I had also tried using 'adjustNothing' and doing the following, but this doesn't appear to work either as the EditText height won't get changed until the number of lines are changed and the number of lines are only changed when it knows whether or not the keyboard is open or closed.
private void setupListeners() {
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View mainView = (LinearLayout) findViewById(R.id.add_record);
int heightDiff = mainView.getHeight() - noteEditText.getHeight();
Log.d("KB", "HeightDiff: " + heightDiff);
if (heightDiff > 1000 || keyboardShown) { // 99% of the time the height diff will be due to a keyboard.
Log.d("KB", "Keyboard is open");
if (isKeyboardVisible) {
noteEditText.setLines(12);
noteEditText.setCursorVisible(true);
noteEditText.requestLayout();
isKeyboardVisible = false;
}
} else {
Log.d("KB", "Keyboard is closed");
if (!isKeyboardVisible) {
noteEditText.setLines(50);
noteEditText.setCursorVisible(false);
noteEditText.requestLayout();
isKeyboardVisible = true;
}
}
}
});
noteEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
numTimesClicked++;
Log.d("KB", "onClick: " + numTimesClicked);
if (clicked) {
// Run function
Log.d("KB", "clicked");
InputMethodManager imm = (InputMethodManager) AddActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
Log.d("KB", "Software Keyboard was shown");
isKeyboardVisible = true;
keyboardShown = true;
} else {
Log.d("KB", "Software Keyboard was not shown");
isKeyboardVisible = false;
keyboardShown = false;
}
} else {
Log.d("KB", "scroll");
clicked = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
clicked = false;
}
}, 3 * 1000);
}
}
});
noteEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.d("KB", "closeKeyboard");
noteEditText.setLines(50);
noteEditText.setCursorVisible(false);
noteEditText.requestLayout();
isKeyboardVisible = false;
}
return false;
}
});
Therefore, how would I go about achieving the desired effect of adjusting the EditText lines (when the keyboard is opened and closed) and not pushing other content out of view? So then the Support ActionBar and first EditText always remain in the same position and only the second EditText is adjusted when the soft keyboard appears?