material-components / material-components/material-components-android
[BottomSheetDialogFragment] Listener set with setOnApplyWindowInsetsListener is never called
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
Description:
I want to adjust the padding of my root view when the keyboard opens and I'm trying to accomplish it using setOnApplyWindowInsetsListener().
In a BottomSheetDialogFragment, the listener is never called.
If I change my fragment to extend from DialogFragment, the listener is called and the behavior is as expected.
Expected behavior:
The listener set with setOnApplyWindowInsetsListener() is called in a BottomSheetDialogFragment and applies padding to the layout.
Source code:
package com.minasmina.bottomsheetdialogfragmentbug
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dialog = TestDialog()
dialog.show(supportFragmentManager, null)
}
}
// TODO If you extend from DialogFragment(), `setOnApplyWindowInsetsListener` _is_ called
// and the behavior is correct (padding is applied).
class TestDialog : BottomSheetDialogFragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.test, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val window = requireDialog().window!!
ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets ->
val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom
view.setPadding(0, 0, 0, imeHeight)
insets
}
WindowCompat.setDecorFitsSystemWindows(window, false)
}
}
test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Comment"
android:layout_above="@id/button" />
<com.google.android.material.button.MaterialButton
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Test" />
</RelativeLayout>
Minimal sample app repro:
https://github.com/minas1/BottomSheetDialogFragmentBug
Android API version: 32
Material Library version: 1.6.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked minimal sample app and inspect the BottomSheetDialogFragment path around onViewCreated, setOnApplyWindowInsetsListener(), and WindowCompat.setDecorFitsSystemWindows(). Compare it with the DialogFragment behavior shown in the report. Done means the listener is called for the bottom sheet and the root view receives the expected keyboard padding.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100