luckybiglarge
luckybiglarge

Reputation: 11

How to add margin to Bottom Sheet Fragment like the Youtube app?

I was just wondering how to add margins to bottomsheetfragment using material 3 design like the new version of youtube app.
Here is the screenshot of the bottom sheet fragment that I want to implement bottom sheet example
I have already tried the solution from here Customization Bottom Sheet Dialog's View but it doesn't works correctly with material 3

Here is my bottom sheet fragment layout:


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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".utils.Bottom"\>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_48"
        android:gravity="center_vertical"
        android:text="Thhis is a bottom sheet"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_48"
        android:gravity="center_vertical"
        android:text="Thhis is a bottom sheet"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_48"
        android:gravity="center_vertical"
        android:text="Thhis is a bottom sheet"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_48"
        android:gravity="center_vertical"
        android:text="Thhis is a bottom sheet"/>

Here is my bottom sheet fragment class:

class BottomSheet : BottomSheetDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = View.inflate(context,R.layout.fragment_bottom,null)
     
        return view
    }

}

What should I do to replicate the bottom sheet like the screenshot above?

Upvotes: 1

Views: 373

Answers (1)

Umang
Umang

Reputation: 158

You can do this by setting the custom style to BottomSheetDialogFragment, which makes a transparent background for the bottom sheet.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)
    }

create new style into the styles.xml file

<style name="CustomBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item>
</style>

<style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@android:color/transparent</item>
</style>

create bottomsheet dialog layout file as follow

<androidx.constraintlayout.widget.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"
android:background="#05ffffff">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginEnd="@dimen/_10sdp"
        android:layout_marginBottom="@dimen/_20sdp"
        android:background="@drawable/dialog_bg"
        app:layout_constraintBottom_toTopOf="@id/view1">

        <View
            android:id="@+id/view"
            android:layout_width="@dimen/_60sdp"
            android:layout_height="@dimen/_3sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:background="@drawable/rounded_corner"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Save To Watch Later"
            android:textColor="@color/white"
            android:textSize="@dimen/_15ssp"
            android:layout_marginTop="@dimen/_10sdp"
            app:layout_constraintTop_toBottomOf="@id/view" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Save To Watch Later"
            android:textColor="@color/white"
            android:textSize="@dimen/_15ssp"
            android:layout_marginTop="@dimen/_10sdp"
            app:layout_constraintTop_toBottomOf="@id/text1" />

        <TextView
            android:id="@+id/text3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Save To Watch Later"
            android:textColor="@color/white"
            android:textSize="@dimen/_15ssp"
            android:layout_marginTop="@dimen/_10sdp"
            app:layout_constraintTop_toBottomOf="@id/text2" />

        <TextView
            android:id="@+id/text4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="@dimen/_10sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:text="Save To Watch Later"
            android:textColor="@color/white"
            android:textSize="@dimen/_15ssp"
            app:layout_constraintTop_toBottomOf="@id/text3" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:id="@+id/view1"
        android:layout_width="@dimen/_60sdp"
        android:layout_height="@dimen/_3sdp"
        android:layout_marginBottom="@dimen/_20sdp"
        android:background="@drawable/rounded_corner"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

set 5 percent of transparency background color to the main layout and set the dialog background to the inner layout.

Upvotes: 0

Related Questions