How to auto scroll up the chat messages (using recyclerview instead of listview) above the softkeypad whenever keypad pop ups?

Kalaiselvan picture Kalaiselvan · Sep 21, 2015 · Viewed 8.5k times · Source

Like in chat applications whenever we want to send messages soft keypad pop ups which also auto scroll the last seen messages to top of the soft keypad provided that nothing is hidden behind the soft keypad. But in my case the keypad hides the conversations. How to fix this issue and I have used to recycler view for displaying the messages. I used android.support.v7.widget.RecyclerView

Answer

Saeed Entezari picture Saeed Entezari · Sep 21, 2015

With RecyclerView you can achieve this as follows:

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);

This block of code will tell the RecyclerView to scroll relative to the bottom of the list but also it shows the messages in reverse order so in your code if you are getting the messages from database, read them from the last message like this:

Cursor c = ...;
c.moveToLast();
do{
//your code which gets messages from cursor...
}while(c.moveToPrevoius());

and when you want to add a new message to the list just add them like this:

//ArrayList messages
messages.add(0, message);