androidandroid-softkeyboard

Adjusting Layout when Softkeyboard open on pixel 7 devices


In the chat screen, LAYOUT1. When keyboard open the top view and recyclerview stays where they are and bottom Layout with edit text is adjusted. On redmi, poco and Samsung devices. But it doesn't work on pixel 7 device the keyboard appear on top of layout

       <activity
        android:name=".ActivityChat"
        android:exported="true"
        android:screenOrientation="portrait" />

enter image description here

when I use

 android:windowSoftInputMode="adjustPan"

The entire layout is scroll up and bottom Layout is not fully visible. Edittext is focused. It doesn't look good for all devices. I want the first scenario on pixel 7.

picture 2 with adjustpan

Here is my XML code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_gradient">


<RelativeLayout
    android:id="@+id/rlTop"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/_40sdp">

            <ImageView
                android:id="@+id/imgBack"
                android:layout_width="@dimen/_36sdp"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"

                android:layout_gravity="start"
                android:paddingHorizontal="@dimen/_10sdp"
                android:src="@drawable/back_arrow"
                app:tint="@color/white" />

            <ImageView
                android:id="@+id/iconHistory"
                android:layout_width="@dimen/_30sdp"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
                android:layout_toStartOf="@id/iconEdit"

                android:paddingHorizontal="@dimen/_7sdp"
                android:src="@drawable/icon_history"
                app:tint="@color/white" />

            <ImageView
                android:id="@+id/iconEdit"
                android:layout_width="@dimen/_36sdp"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_centerVertical="true"

                android:paddingHorizontal="@dimen/_10sdp"
                android:src="@drawable/icon_edit"
                app:tint="@color/white" />

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toEndOf="@id/imgBack"
                android:text="Ask AI"
                android:textColor="@color/white"
                android:textSize="@dimen/_15ssp"
                android:textStyle="bold" />


        </RelativeLayout>
    </RelativeLayout>


</RelativeLayout>


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_above="@id/rlBottom"
    android:layout_below="@id/rlTop" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rvList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/rlTop"
    android:elevation="@dimen/_4sdp"
    android:visibility="gone" />

<LinearLayout
    android:id="@+id/rlBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="#382E4C"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvRetry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/_6sdp"
        android:background="@drawable/bg_retry"
        android:paddingHorizontal="@dimen/_8sdp"
        android:paddingVertical="@dimen/_1sdp"
        android:text="Retry"
        android:textColor="@color/black"
        android:visibility="gone" />


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/_52sdp">

        <ImageView
            android:id="@+id/icon1"
            android:layout_width="@dimen/_20sdp"
            android:layout_height="match_parent"
            android:layout_marginHorizontal="@dimen/_7sdp"
            android:paddingHorizontal="@dimen/_1sdp"
            android:src="@drawable/attach"
            android:visibility="gone" />

        <EditText
            android:id="@+id/textBox"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginVertical="@dimen/_9sdp"
            android:layout_marginStart="@dimen/_10sdp"
            android:layout_toStartOf="@id/microphone"
            android:layout_toEndOf="@id/icon1"
            android:background="@drawable/stroke_eddittext"
            android:hint="Whats on your mind?"
            android:lines="1"
            android:paddingHorizontal="@dimen/_15sdp"
            android:singleLine="true"
            android:textColor="@color/white"
            android:textColorHint="@color/hintText"
            android:textSize="@dimen/_11ssp" />

        <ImageView
            android:id="@+id/microphone"
            android:layout_width="@dimen/_20sdp"
            android:layout_height="match_parent"
            android:layout_marginHorizontal="@dimen/_4sdp"
            android:layout_toStartOf="@+id/send"
            android:paddingHorizontal="@dimen/_3sdp"
            android:src="@drawable/icon_microphone"
            app:tint="@color/iconColor" />


        <ImageView
            android:id="@+id/send"
            android:layout_width="@dimen/_22sdp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginStart="@dimen/_4sdp"
            android:layout_marginEnd="@dimen/_8sdp"
            android:adjustViewBounds="true"
            android:src="@drawable/img_send2" />
       
    </RelativeLayout>


</LinearLayout>

</RelativeLayout>

Solution

  • By enabling edge to edge stop default activity padding on all devices.

        enableEdgeToEdge()
    

    this listener will be called when edge to edge is enabled and you need to manually set padding when keyboard opens

            ViewCompat.setOnApplyWindowInsetsListener(binding!!.main) { view, windowInsets ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemGestures())
    
            val imeInsets = windowInsets.getInsets(WindowInsetsCompat.Type.ime())
            val isKeyboardVisible = imeInsets.bottom > 0
            val keyboardHeight = imeInsets.bottom
    
            if (isKeyboardVisible) {
                Log.e("KeyboardStatus", "Keyboard opened. Height: $keyboardHeight")
    
                view.updatePadding(0, 0, 0, keyboardHeight)
                moveToBottom()
            } else {
                view.updatePadding(0, 0, 0, insets.bottom)
                Log.e("KeyboardStatus", "Keyboard closed")
            }
             WindowInsetsCompat.CONSUMED
        }
    

    This now works both on Pixel 7 and Xiaomi devices.