danial
danial

Reputation: 65

windowSoftInputMode adjustResize doesn't work in target sdk 35

When I increased the target SDK of the app from 34 to 35, the windowSoftInputMode in adjustResize mode no longer worked. I have tested everything and found that it is only related to the target SDK. For example, I tested it on different devices with different versions of Android and it didn't work, I tested it in several activities with different layouts, I tested it in different windowSoftInputMode modes, I also tested fitsSystemWindows. It didn't work in any case and I'm pretty sure it's related to target SDK 35. But after hours of trying I didn't find any solution for it.

The problem is this: I have an activity with the value android:windowSoftInputMode="adjustResize" in the manifest, which in target SDK 35, when the keyboard is opened, the layout is not placed on top of the keyboard and the keyboard covers the screens, but in target SDK 34 and below, this problem does not exist.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:focusable="true"
    android:fitsSystemWindows="true"
    android:focusableInTouchMode="true"
    tools:context=".view.RegisterNameActivity">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/back"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginStart="@dimen/margin_10"
        android:layout_marginTop="@dimen/margin_10"
        android:background="?selectableItemBackgroundBorderless"
        android:clickable="true"
        android:focusable="true"
        android:padding="@dimen/padding_6"
        android:tint="?colorOnSurface"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_back_24" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/iran_sans_demi_bold"
        android:text="@string/your_name"
        android:textColor="?colorOnSurface"
        android:textSize="@dimen/text_26"
        app:layout_constraintBottom_toTopOf="@+id/description"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4"
        app:layout_constraintVertical_chainStyle="packed" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_32"
        android:layout_marginTop="@dimen/margin_4"
        android:layout_marginEnd="@dimen/margin_32"
        android:gravity="center"
        android:paddingStart="@dimen/padding_8"
        android:paddingEnd="@dimen/padding_8"
        android:text="@string/your_name_and_bio_description"
        android:textColor="?colorOnSurfaceVariant"
        android:textSize="@dimen/text_14"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toTopOf="@+id/name"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title" />

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_32"
        android:layout_marginTop="@dimen/margin_32"
        android:layout_marginEnd="@dimen/margin_32"
        android:backgroundTint="?colorOnSurface"
        android:gravity="start|center_vertical"
        android:hint="@string/name_required"
        android:imeOptions="flagNoExtractUi"
        android:paddingStart="@dimen/padding_8"
        android:paddingEnd="36dp"
        android:singleLine="true"
        android:textDirection="locale"
        app:layout_constraintBottom_toTopOf="@+id/bio"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/description"
        tools:ignore="RtlHardcoded" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/nameLength"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-4dp"
        android:layout_marginEnd="@dimen/margin_4"
        android:textColor="?colorOnSurfaceVariant"
        android:textSize="@dimen/text_10"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="@+id/name"
        app:layout_constraintTop_toBottomOf="@+id/name"
        tools:text="0/25" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/nameError"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_8"
        android:layout_marginTop="-4dp"
        android:layout_marginEnd="@dimen/margin_8"
        android:textColor="?colorError"
        android:textSize="@dimen/text_10"
        android:visibility="gone"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/nameLength"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="@+id/name"
        app:layout_constraintTop_toBottomOf="@+id/name"
        tools:text="Please enter your name"
        tools:visibility="visible" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/clearName"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginEnd="-3dp"
        android:padding="10dp"
        android:tint="?colorOnSurfaceVariant"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/name"
        app:layout_constraintEnd_toEndOf="@+id/name"
        app:layout_constraintTop_toTopOf="@+id/name"
        app:srcCompat="@drawable/ic_close_16"
        tools:visibility="visible" />

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/bio"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_16"
        android:backgroundTint="?colorOnSurface"
        android:gravity="start|center_vertical"
        android:hint="@string/biography_optional"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textMultiLine"
        android:textDirection="locale"
        android:paddingStart="@dimen/padding_8"
        android:paddingEnd="36dp"
        app:layout_constraintBottom_toTopOf="@+id/termsAndConditions"
        app:layout_constraintEnd_toEndOf="@+id/name"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="@+id/name"
        app:layout_constraintTop_toBottomOf="@+id/name"
        tools:ignore="RtlHardcoded" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/clearBio"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginEnd="-3dp"
        android:padding="10dp"
        android:tint="?colorOnSurfaceVariant"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/bio"
        app:layout_constraintEnd_toEndOf="@+id/bio"
        app:layout_constraintTop_toTopOf="@+id/bio"
        app:srcCompat="@drawable/ic_close_16"
        tools:visibility="visible" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/bioLength"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="-4dp"
        android:layout_marginEnd="@dimen/margin_4"
        android:textColor="?colorOnSurfaceVariant"
        android:textSize="@dimen/text_10"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toEndOf="@+id/bio"
        app:layout_constraintTop_toBottomOf="@+id/bio"
        tools:text="0/256" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/termsAndConditions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_32"
        android:layout_marginEnd="@dimen/margin_32"
        android:layoutDirection="ltr"
        android:textColor="?colorOnSurfaceVariant"
        android:textSize="@dimen/text_12"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="@+id/submitFab"
        app:layout_constraintEnd_toStartOf="@+id/submitFab"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/submitFab"
        tools:text="By registering, you agree to the terms and conditions of using the App." />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/submitFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/margin_24"
        android:clickable="true"
        android:focusable="true"
        android:layoutDirection="ltr"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:rippleColor="?colorOnPrimaryContainer"
        app:srcCompat="@drawable/ic_done_24"
        tools:ignore="ContentDescription,NotSibling" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 112

Answers (1)

Blo
Blo

Reputation: 11988

Since Android 15 (Android API 35), apps are now edge-to-edge by default. With this change, adjustResize is not enough by itself.

As this article describes:

Before targeting SDK 35, using android:windowSoftInputMode=”adjustResize” was all you needed to maintain focus on — for example — an EditText in a RecyclerView when opening an IME. With “adjustResize”, the framework treated the IME as the system window, and the window’s root views were padded so content avoids the system window.

After targeting SDK 35, you must also account for the IME using ViewCompat.setOnApplyWindowInsetsListener and WindowInsetsCompat.Type.ime() because the framework will not pad the window’s root views.

Therefore, in ordre to maintain focus on your EditText when the keyboard is shown, you have to add the ime() type in the insets padding. Then when you set the insets on your views, you have to also get this type as follows:

ViewCompat.setOnApplyWindowInsetsListener(this) { view, insets ->
    val bars = insets.getInsets(
        WindowInsetsCompat.Type.systemBars()
                or WindowInsetsCompat.Type.displayCutout()
                or WindowInsetsCompat.Type.ime() // adding the ime's height
    )
    view.updatePadding(
        left = bars.left, 
        top = bars.top, 
        right = bars.right, 
        bottom = bars.bottom
    )
    WindowInsetsCompat.CONSUMED
}

Upvotes: 1

Related Questions