android – How to push recyclerView up when keyboard appear?-ThrowExceptions

Exception or error:

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.

How to solve:

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);

}
    });

###

The way I did it, is to set setStackFromEnd() according the size of the item holder, and set adjustResize in androidManifest.

This is how I check:

if (recycleView.getChildCount() == items.size()){
    mLayoutManager.setStackFromEnd(true);
}
else{
    mLayoutManager.setStackFromEnd(false);
}

###

I am not good at English.
Use ChatRecyclerView and set the stackfromend of the linearlayoutmanager to false.

public class ChatRecyclerView extends RecyclerView {
    private int oldHeight;

    public ChatRecyclerView(Context context) {
        super(context);
    }

    public ChatRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChatRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int delta = b - t - this.oldHeight;
        this.oldHeight = b - t;
        if (delta < 0) {
            this.scrollBy(0, -delta);
        }
    }
}

###

I got the solution from Push up content when clicking in edit text

Just put below code in

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Leave a Reply

Your email address will not be published. Required fields are marked *