skip to Main Content

I want to make chat page like Telegram, when keyboard appears layout scrolling up but only body not app bar. Also after keyboard appears, recycler view must be scrollable and when keyboard disappear layout must scroll down. Sorry my poor English. Thanks.

I tried scroll view and

scrollLastPosition = myLayoutManager.findLastVisibleItemPosition()
scrollFirstPosition = myLayoutManager.findFirstVisibleItemPosition()


private fun layoutChanger() {
    binding.messageRecycler.addOnLayoutChangeListener { _, _, _, _, bottom,
                                                        _, _, _, oldBottom ->

        if (scrollLastPosition == messageArrayList.size - 1 && bottom < oldBottom) {
            binding.messageRecycler.smoothScrollToPosition(messageArrayList.size - 1)
        } else if (scrollLastPosition < messageArrayList.size - 1 && bottom != oldBottom) {
            if (bottom > oldBottom) {
                binding.messageRecycler.postDelayed({
                    binding.messageRecycler.scrollToPosition(scrollFirstPosition)
                }, 0)
            }
            binding.messageRecycler.postDelayed({
                binding.messageRecycler.smoothScrollToPosition(scrollLastPosition)
            }, 10)         

//binding.messageRecycler.smoothScrollToPosition(scrollLastPosition)
        }
    }
}

My layout

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.core.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    tools:context=".view.MessageActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/messageRecycler"
            android:layout_width="match_parent"
            android:layout_height="640dp"
            app:layout_constraintBottom_toTopOf="@+id/bottom"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

        </androidx.recyclerview.widget.RecyclerView>

        <LinearLayout
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_below="@id/messageRecycler"
            android:layout_alignParentBottom="true"
            android:background="@drawable/back"
            android:gravity="center"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <EditText
                android:id="@+id/writeMessage"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="6"
                android:hint="@string/type_here"
                android:importantForAutofill="no"
                android:inputType="text"
                android:padding="5dp"
                tools:ignore="Suspicious0dp">

            </EditText>

            <ImageView
                android:id="@+id/sendImage"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_gravity="center"
                android:layout_marginEnd="5dp"
                android:layout_weight="1"
                android:background="@drawable/send"
                android:contentDescription="@string/todo">

            </ImageView>
        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>
</layout>

but nothing is work like Telegram.In this layout it works but after keyboard appears, when I scroll recycler view edit text also scolling down and disappear, I want that edit text doesn’t move.

2

Answers


  1. In the layout file, based on your requirements, using NestedScrollView tag is counter-productive, removing that layout and directly making ConstraintLayout the root view should solve your problem.

    Login or Signup to reply.
  2. Please Remove the NestedScrollView so that your layout looks something like this

    <?xml version="1.0" encoding="utf-8"?>
    
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/messageRecycler"
                android:layout_width="match_parent"
                android:layout_height="640dp"
                app:layout_constraintBottom_toTopOf="@+id/bottom"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent">
    
            </androidx.recyclerview.widget.RecyclerView>
    
            <LinearLayout
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_below="@id/messageRecycler"
                android:layout_alignParentBottom="true"
                android:background="@drawable/back"
                android:gravity="center"
                android:orientation="horizontal"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent">
    
                <EditText
                    android:id="@+id/writeMessage"
                    android:layout_width="0dp"
                    android:layout_height="60dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_weight="6"
                    android:hint="@string/type_here"
                    android:importantForAutofill="no"
                    android:inputType="text"
                    android:padding="5dp"
                    tools:ignore="Suspicious0dp">
    
                </EditText>
    
                <ImageView
                    android:id="@+id/sendImage"
                    android:layout_width="0dp"
                    android:layout_height="45dp"
                    android:layout_gravity="center"
                    android:layout_marginEnd="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/send"
                    android:contentDescription="@string/todo">
    
                </ImageView>
            </LinearLayout>
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    </layout>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search