Search code examples
androidkotlinbottomsheetdialogfragment

Can't get BottomSheetDialogFragment expanded to top of the screen when opened


I can't get my bottom shee dialog fragment expanded to top of the screen. It is always like 60% of the screen or less if I set layout_height (for test) on some lower value. I have tried set it on 600, 700, 800 (currently 900dp) and it is still no higher then on the screenshot below (there are 4 textviews just for test and they are not visible on the screen):

60% bottom sheet

My bottom sheet fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainLayotCoordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_gravity="fill_vertical"
    android:fitsSystemWindows="true"
    app:layout_anchorGravity="top">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/mainLayoutConstraint"
        android:layout_width="match_parent"
        android:layout_height="900dp"
        android:fitsSystemWindows="false"
        app:layout_anchorGravity="top"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <com.safetyheads.akademiaandroida.MyTest2View
            android:id="@+id/myTest2View"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:hint="type something"
            app:layout_constraintBottom_toTopOf="@+id/textView4"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:name="First nameno"
            app:visibility="0">

        </com.safetyheads.akademiaandroida.MyTest2View>

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/myTest2View" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.503"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView4" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView5" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.503"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView7" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

My bottom sheet dialog fragment class:


import android.annotation.SuppressLint
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.Nullable
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.safetyheads.akademiaandroida.databinding.FragmentBottomSheetWriteToUsBinding

class BottomSheetWriteToUsFragment :BottomSheetDialogFragment() {

    private lateinit var binding: FragmentBottomSheetWriteToUsBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentBottomSheetWriteToUsBinding.inflate(inflater, container, false)
        return binding.root
    }

    @SuppressLint("ResourceType")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        bottomSheetDialog.setOnShowListener { dialog: DialogInterface ->
            val bottomSheet = (dialog as BottomSheetDialog).findViewById<ConstraintLayout>(R.id.mainLayoutConstraint)
            if (bottomSheet != null) BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_EXPANDED
        }
        return bottomSheetDialog
    }
}

There is some code from a solution I have found with BottomSheetBehavior.STATE_EXPANDED but it is not making my botom sheet go to the top as I expected.

I can drag it up manually and then it is on full screen.

Does anyone, please help me to make that bottom sheet dialog fragment go to the top of the screen (like fulscreen) automatically after opening?


Solution

  • Here is my code.

    <?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"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="15dp"
            android:background="@drawable/bottom_sheet_background"
            >
        ...
        </RelativeLayout>
    </RelativeLayout>
    
    private void showBottomSheetDialog() {
        bottomSheetDialog = new BottomSheetDialog(activity);
        bottomSheetDialog.setContentView(R.layout.pickup_bottom_sheet_layout);
        ...
        bottomSheetDialog.show();
    }