ganeshraj020794
ganeshraj020794

Reputation: 186

How to programmatically scroll a recyclerview over keyboard?

I have a recyclerview in which there is an edit text where it will hidden and opens up on click of some button. Please refer to screen 1.

On click of reject or accept the view is displayed:

on click of reject or accept the view is displayed

Now, my recyclerview is expanding downwards but I want it to expand upward like this:

By default, it expands downward:

by default, it expands downward

I want it to expand upward like this:

I want it to expand upward like this

Similarly, when keyboard opens up I want the whole recyclerview cell just above the keyboard like this.

By default, it does not move upward:

by default, it does not move upward

I want something like this:

I want something like this

What should be the approach for this functionality?
Do I have to do the calculations of cell height, keyboard height or is there some other method for this.

Please attach related links.
Thanks

Upvotes: 0

Views: 502

Answers (2)

Taha wakeel
Taha wakeel

Reputation: 169

I wrote this for my chat class you can rewrite this code snippet according to yours.

private boolean keyboardShown(View rootView) {

    final int softKeyboardHeight = 100;
    Rect r = new Rect();
    rootView.getWindowVisibleDisplayFrame(r);
    DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
    int heightDiff = rootView.getBottom() - r.bottom;
    return heightDiff > softKeyboardHeight * dm.density;
}

messageEditText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (keyboardShown(messageEditText.getRootView())) {
                Log.d("keyboard", "keyboard UP");

                if (keyboardUp == false) {
                    if (results.size() > 0)
                        chatList.smoothScrollToPosition(results.size()+1);
                    keyboardUp = true;
                }

            } else {
                Log.d("keyboard", "keyboard Down");
                keyboardUp = false;
            }
        }
    });

Upvotes: 1

Yosef
Yosef

Reputation: 1303

Let's divide the question into two, scroll down the RecyclerView and show the cell when the keyboard is shown.

  1. just use the scrollToPosition(index) with to the cell index

  2. you can just change the android:windowSoftInputMode to be adjustSize in the activity:

    <activity
    android:windowSoftInputMode="adjustResize"
    android:name=".MainActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    

or just in the fragment:

        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

Upvotes: 0

Related Questions