chrisbanes / insetter

Insetter is a library to help apps handle WindowInsets more easily
https://chrisbanes.github.io/insetter
Apache License 2.0
1.13k stars 42 forks source link

Keyboard inset padding and NestedScrollView #93

Closed GeniusRUS closed 3 years ago

GeniusRUS commented 3 years ago

@chrisbanes hi! A strange situation occurred while working with keyboard insets If you install paddings from the keyboard for NestedScrollView, the view in focus does not remain visible, hiding behind the keyboard How can you handle this scenario so that the EditText respects the keyboard that appears? Only by setting margins for NestedScrollView?

Activity and layout code is below

class MainActivity : AppCompatActivity(R.layout.activity_main) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val rootView = findViewById<LinearLayout>(R.id.root_view)
        val nestedScroll = findViewById<NestedScrollView>(R.id.nested_scroll)
        val toolbar = findViewById<Toolbar>(R.id.toolbar)

        WindowCompat.setDecorFitsSystemWindows(window, false)

        Insetter.builder().setOnApplyInsetsListener { _, insets, _ ->
            toolbar.updatePadding(
                top = insets.getInsets(WindowInsetsCompat.Type.systemBars()).top
            )
            nestedScroll.updatePadding(
                bottom = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom + insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom
            )
        }.applyToView(rootView)
    }
}

<LinearLayout 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:id="@+id/root_view" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="@string/app_name" />

<androidx.core.widget.NestedScrollView
    android:id="@+id/nested_scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="750dp"
            android:layout_marginBottom="120dp"
            android:hint="This will be hide by the keyboard" />
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

https://user-images.githubusercontent.com/24453308/107341477-1a2f9580-6ad0-11eb-82ab-50a80642f30d.mp4

chrisbanes commented 3 years ago

First of all, this will look a LOT better once I release the new update (which should be later this week):

toolbar.applyInsetter {
    type(statusBars = true) {
        padding(top = true)
    }
}

nestedScroll.applyInsetter {
    type(ime = true, systemBars = true) {
        padding(bottom = true)
    }
}

You can try the snapshots now.

As to the issue, does the EditText need to be in the ScrollView? If so, I'd add the ime + systemBars padding to the LinearLayout instead.

GeniusRUS commented 3 years ago

@chrisbanes thanks for the answer. I tried to set paddings for LinearLayout, unfortunately, this did not lead to anything, I got absolutely the same behavior I think that if NestedScrollView does not change its external dimensions in the layout, then the content in it will always ignore any padding and focused view will be also ignored

I can also provide a minimal project in order to reproduce the problem

chrisbanes commented 3 years ago

If you have a sample, I’ll try and take a look.

I don’t think this is anything to do with the library though, so I’ll close this.

GeniusRUS commented 3 years ago

Thanks for the help Here is a MVP to reproduce

Insets.zip

chrisbanes commented 3 years ago

Had a quick look at this. The issue is that the EditText is in the ScrollView. There's not a whole lot you can do here really as you need to also scroll the ScrollView.

GeniusRUS commented 3 years ago

Thanks for your help! And last question: Is there any plans for some tools to make this case work?

chrisbanes commented 3 years ago

No, not that I'm aware of.