For achieving the behaviour of scrolling the list upside along with keyboard, I derived the solution from here: https://stackoverflow.com/a/34103263/1739882
And to add custom behaviour that decides whether to scroll the list or not I fetched the last visible item with this:
linearLayoutManager.findLastVisibleItemPosition()
and used a flag to handle the scenario. Here is the complete code:
//Setup editText behavior for opening soft keyboard
activityChatHeadBinding.edtMsg.setOnTouchListener((v, event) -> {
InputMethodManager keyboard = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (keyboard != null) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) activityChatHeadBinding.recyclerView.getLayoutManager();
isScrollToLastRequired = linearLayoutManager.findLastVisibleItemPosition() == chatNewAdapter.getItemCount() - 1;
keyboard.showSoftInput(findViewById(R.id.layout_send_msg), InputMethodManager.SHOW_FORCED);
}
return false;
});
//Executes recycler view scroll if required.
activityChatHeadBinding.recyclerView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
if (bottom < oldBottom && isScrollToLastRequired) {
activityChatHeadBinding.recyclerView.postDelayed(() -> activityChatHeadBinding.recyclerView.scrollToPosition(
activityChatHeadBinding.recyclerView.getAdapter().getItemCount() - 1), 100);
}
});