0

I have the following BottomSheetDialogFragment

package com.example.testbottomsheetheight

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.rememberScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class TestBottomSheetDialogFragment : BottomSheetDialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View = ComposeView(requireContext()).apply {
        setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
        setContent {
            Screen()
        }
    }

    @Composable
    private fun Screen() {
        val scaffoldState = rememberScaffoldState()
        Scaffold(
            modifier = Modifier,
            contentColor = Color.Cyan,
            backgroundColor = Color.Blue,
            scaffoldState = scaffoldState,
        ) {
            Column(
                modifier = Modifier.padding(it),
            ) {
                Text("Hello world!")
            }
        }
    }
}

Notice I'm adding just a Text component at the end, but look how tall the bottom sheet shows up:

Screenshot of the tall bottom sheet

I know if I force a size on the Scaffold modifier, I can get something smaller like that below. Notice it ignores the width, but I can change the height as I want:

modifier = Modifier.size(
    width = 0.dp,
    height = 50.dp,
),

Screenshot of the bottom sheet with a forced heigh

My problem is that the content inside the Column is dynamic, so I can't just force a height on its Scaffold. So I would like to know if there is something like "wrap_content" that I could use here to make the bottom sheet as tall as necessary but no taller than it.


Below the layout and activity files in case you want to reproduce:

package com.example.testbottomsheetheight

import android.os.Bundle
import android.widget.Button
import androidx.fragment.app.FragmentActivity

class MainActivity : FragmentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)
        findViewById<Button>(R.id.show_bottom_sheet).setOnClickListener {
            TestBottomSheetDialogFragment().show(supportFragmentManager, "TestTag")
        }
    }
}
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/show_bottom_sheet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show bottom sheet"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
ademar111190
  • 14,215
  • 14
  • 85
  • 114

0 Answers0