How to push recyclerView up when keyboard appear?

david picture david · Sep 10, 2015 · Viewed 10.5k times · Source

I want to build a message layout as in whatsapp. I have an edittext and a recyclerView.

the problem is when the keyboard appear it hide the messages!

So let's say this the RecyclerView:

---item 1---  
---item 2---  
---item 3---  
---item 4---  
---EditText---

when the the keyboard appear, I get this:

---item 1---  
---item 2---  
---EditText---
---Keyboard---  

but I want to get this:

---item 3---  
---item 4---  
---EditText---
---Keyboard---

NOTE: when I set linearLayoutManager.setStackFromEnd(true); it works but when there is one message it appears at the bottom of the page.

Answer

devdoot Ghosh picture devdoot Ghosh · May 26, 2016

set adjustresize for the activity with recyclerview and editText :

android:windowSoftInputMode="adjustResize"

add onLayoutChangeListener to your RecyclerView and set scrollToPosition to data.size() -1 in onLayoutChange:

  mRecyclerView.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)
{

mRecyclerView.scrollToPosition(mMessages.size()-1);

}
    });