3

I have a BottomSheet which houses a product detail card. The problem is, when I click on the + or - button on the product detail while the bottom sheet is in it's Expanded state, it jumps down.

When it is down and I click on the buttons it doesn't jump, it only happens when it is in it's Expanded (completely up) state

I have attached a GIF to show what is exactly happening

enter image description here

Here is the code

scan_sheet.xml

<?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:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:animateLayoutChanges="false"
    android:background="@drawable/bottom_sheet_dialog_fragment"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="studio.monotype.storedemo.BottomSheetBehavior">

    <include
        layout="@layout/hero_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/divider_view"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="44dp"
        android:layout_marginEnd="24dp"
        android:background="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/hero_item" />

    <include
        layout="@layout/related_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider_view"
        tools:layout_editor_absoluteX="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

ScanActivity.kt (simplified to show only what is essential)

class ScanActivity : AppCompatActivity() {


    private lateinit var bottomSheet: BottomSheetBehavior<*>
 

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_scan)

        setupBottomSheet()
        
        showSheet()
    }

  


    private fun setupBottomSheet() {
        bottomSheet = BottomSheetBehavior.from(bottom_sheet)
        bottomSheet.isHideable = true
        bottomSheet.skipCollapsed= true
        bottomSheet.isDraggable = true
        bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN
        bottomSheet.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback {
            override fun onSlide(bottomSheet: View, slideOffset: Float) {
            }

            @SuppressLint("SwitchIntDef")
            override fun onStateChanged(sheet: View, newState: Int) {
                when (newState) {
                    BottomSheetBehavior.STATE_HIDDEN -> {
                        codeScanner.startPreview()
                    }
                }
            }

        })
        
        
         plus_btn.setOnClickListener {
            var qty= qty_tv.text.toString().toInt()
            qty++
            qty_tv.text =qty.toString()
        }

        minus_btn.setOnClickListener {
            var qty= qty_tv.text.toString().toInt()
            if(qty!=0)
            {
                qty--
            }
            qty_tv.text =qty.toString()
        }


    }

    private fun showSheet() {
        bottomSheet.state = BottomSheetBehavior.STATE_EXPANDED
    }

   
}
Prateek Prasad
  • 827
  • 2
  • 10
  • 19
  • `@PrateekPhoenix` , have you found any solving to this? – ghita Apr 23 '19 at 07:37
  • 1
    No not really, we scrapped off that design because it was an issue with the component and there's not been any update to it since. – Prateek Prasad Apr 24 '19 at 08:02
  • Please check this answer https://stackoverflow.com/questions/44943448/android-bottomsheet-jumps-up-when-content-changes, it helped me. – Limun Jan 23 '23 at 10:03

2 Answers2

3

it seems that google engineer gave correct answer

Seems like something is going on because you are setting android:layout_gravity="bottom" on the view with the BottomSheetBehavior. You should remove that line.

It helped on my case

1

Looks to me like that could be a bug in the BottomSheetBehavior? Seems like the height of the sheet isn't getting saved or restored correctly. After the button is pressed, a layout happens again which changes the height. Could you file a bug at https://issuetracker.google.com/issues/new?component=439535

Cameron Ketcham
  • 7,966
  • 2
  • 28
  • 27
  • Hi Cameron, thank you for your reply....just reported it! Here's the link - https://issuetracker.google.com/issues/122017052 – Prateek Prasad Dec 27 '18 at 07:23
  • `@CameronKetcham` I am facing the same issue, even I have set a fixed height dimension. – ghita Apr 23 '19 at 07:32