Как добавить затемнениие фона при использовании BottomSheet

Использую BottomSheet и хочу сделать затемнение фона, как это сделать?

// немного кода:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context="ScheduleShower">

    <include layout="@layout/schedule_shower_content_main" />


    <include layout="@layout/bottom_sheet" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

// bottom_sheet.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_share"
    android:orientation="vertical"
    android:padding="20dp"
    app:behavior_hideable="false"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/ttcommons_light"
                android:gravity="center"
                android:text="@string/share"
                android:textColor="@color/black"
                android:textSize="25sp" />

            <View
                android:id="@+id/view"
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:layout_marginLeft="10sp"
                android:layout_marginRight="10sp"
                android:background="@color/greyAction" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/ttcommons_light"
                android:text="@string/link"
                android:textSize="20dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/link"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:ellipsize="end"
                    android:fontFamily="@font/ttcommons_light"
                    android:singleLine="true"
                    android:text="fun-denis.site/sharelink/sdfad"
                    android:textColor="#000000"
                    android:textSize="25sp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="right"
                    android:orientation="vertical">

                    <ImageView

                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:clickable="true"
                        android:src="@drawable/ic_copy" />
                </LinearLayout>

            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/ttcommons_light"
                android:text="@string/qr_code"
                android:textSize="20dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/qrImage"
                    android:layout_width="160dp"
                    android:layout_height="160dp"
                    android:src="@drawable/mqr" />

            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/otherMethodsTXT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:fontFamily="@font/ttcommons_light"
        android:gravity="center"
        android:text="@string/other_methods"
        android:textColor="@color/mnC"
        android:textSize="20sp" />

</LinearLayout>

Ответы (2 шт):

Автор решения: Circassian
  1. Добавляешь в CoordinatorLayout свой Blackout (Допустим это Framelayout c черным бэкграундом, прозрачностью 0.5 и видимостью Gone)
  2. Когда показываешь BottomSheet, делаешь видимость Visible и с помощью аниматора меняешь прозрачность с 0 до 0.5, делаешь isClickable = true и isFocusable = true, вешаешь кликлистнер и по нажатию на Blackout закрываешь BottomSheet
  3. Когда убираешь BottomSheet, меняешь прозрачность с 0.5 до 0. Когда анимация заканчивается, делаешь видимость Gone, isClickable = false и isFocusable = false
→ Ссылка
Автор решения: garik01

Дополню ответ выше кодом: Добавил свой Blackout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/blackout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0"
    android:background="#000000"
    android:visibility="gone">

</FrameLayout>

Прописал метод, при вызове которого отображается или скрывается затемнение с анимацией

private void blackout(boolean fillBlackout) {
    if (fillBlackout) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(layout_blackout,"alpha",0.5f);
        anim.setDuration(300); // duration 300 ms
        anim.start();
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
        layout_blackout.setVisibility(View.VISIBLE);
        layout_blackout.setClickable(true);
        layout_blackout.setFocusable(true);
        layout_blackout.setEnabled(true);
    } else {
        ObjectAnimator anim = ObjectAnimator.ofFloat(layout_blackout,"alpha",0);
        anim.setDuration(300);
        anim.start();
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                layout_blackout.setVisibility(View.GONE);
                layout_blackout.setClickable(false);
                layout_blackout.setFocusable(false);
                layout_blackout.setEnabled(false);
            }
        });
    }
}

и теперь при изменении состояния, к примеру, BottomSheet, стартую метод выше

modalBottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
     @Override
     public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                blackout(false);
            }
     }
}
→ Ссылка