BottomSheetDialogFragment is not showing up

Viewed 3204

I've followed this tutorial to implement BottomSheetDiaogFragment in my android application.

this is my bottom sheet layout (bottom_sheet.xml):

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<RadioGroup
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="@string/rb1" />

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_weight="1"
        android:text="@string/rb2" />

</RadioGroup>
</android.support.constraint.ConstraintLayout>

BottomSheetDialogFragment class:

class BottomSheetTaskRepeat : BottomSheetDialogFragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.bottom_sheet, container, false)
    }
}

activity:

private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    bottomSheetTaskRepeat.show(supportFragmentManager, "my_bottom_sheet")
}

The problem is that the bottom sheet is not showing up! Any help is appreciated.

3 Answers

Try following:

  1. Create and use the parameterless static method newInstance in your BottomSheetDialogFragment and call the show() method on it.
  2. Try LinearLayoutCompat as a root layout instead of ConstraintLayout.
  3. Try a colourful background to the root layout to get an idea.
  4. Try match_parent height for the root layout.
  5. Make sure that the dismiss() or cancel() is not being called on it immediately.
  6. Check visibility.
  7. If it contains a recyclerView, make sure that it has items, getItemCount is not returning 0, and that we are setting up the values properly!
  8. Restart both: PC and Device if your changes are not reflecting because of Android Studio errors.

This is a late Answer, I am writing for anyone who will face the same problem, This is what I found:

For some reasons non-constrained views heights do not work in BottomSheetDialogFragment. A view whose height is like wrap_content will not show.(But the shadow will be there), but when you specify its height like 80dp it works.

For this question go and change your RadioGroup height and specify it like to:

android:layout_height="200dp"

Hope that is helpful.

UPDATE: Since the default container of BottomSheetDailogFragment is FrameLayout and is set to WRAP_CONTENT, You can override that on your Fragment onStart method like this (Kotlin):

override fun onStart() {
    super.onStart()
    val containerID = com.google.android.material.R.id.design_bottom_sheet
    val bottomSheet: FrameLayout? = dialog?.findViewById(containerID)
    bottomSheet?.let {
        BottomSheetBehavior.from<FrameLayout?>(it).state =
            BottomSheetBehavior.STATE_HALF_EXPANDED
        bottomSheet.layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT
    }

    view?.post {
        val params = (view?.parent as View).layoutParams as (CoordinatorLayout.LayoutParams)
        val behavior = params.behavior
        val bottomSheetBehavior = behavior as (BottomSheetBehavior)
        bottomSheetBehavior.peekHeight = view?.measuredHeight ?: 0
        (bottomSheet?.parent as? View)?.setBackgroundColor(Color.TRANSPARENT)

    }
}

Instead of using supportFragmentManager you have to use childFragmentManager,if you are inflating from inner fragments.

private val bottomSheetTaskRepeat = BottomSheetTaskRepeat()
bottomSheetTaskRepeat.show(childFragmentManager, "my_bottom_sheet")
Related