0

I have created a BottomSheetFragment. Everything is fine except the header of the bottom sheet which is not getting transparent.Is there any way to make the background transparent as like as normal bottomsheetFragment works. I tried by setting the parent background transparent but it is not working at all.

    <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"
    android:id="@+id/layoutContainer"
    android:background="@android:color/transparent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/immediateParent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <View
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
               >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    >

                    <android.support.v4.widget.NestedScrollView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                    </android.support.v4.widget.NestedScrollView>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
        <com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imageView"
            android:layout_width="@dimen/hundrad_twenty"
            android:layout_height="@dimen/hundrad_twenty"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/sixty"
            app:riv_border_color="@color/transparent"
            app:riv_corner_radius="@dimen/twenty"
            app:riv_mutate_background="true"
            app:riv_oval="false" />
        <ImageView
            android:layout_width="@dimen/thirty"
            android:layout_height="@dimen/thirty"
            android:layout_alignParentRight="true"
            android:layout_marginTop="@dimen/hundred_fifty"
            android:layout_marginRight="100dp"/>
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

I have tired this.

ConstraintLayout layoutContainer;
RelativeLayout immediateParent;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = null;
    view = inflater.inflate(R.layout.bottom_sheet_dialog, container);
    ButterKnife.bind(this, view);
    //parent layout transparent not working
    layoutContainer.setBackground(getResources().getDrawable(R.color.transparent));

    immediateParent.setBackground(getResources().getDrawable(R.color.transparent));
   //view transparent 
    view.setBackgroundColor(getResources().getColor(R.color.transparent));
    return view;
}
Ariful Haque
  • 183
  • 1
  • 9
  • "I tried by setting the parent background transparent but it is not working at all" what exactly did you try? (add it to your post) Have you tried using @android:color/transparent for the background color. – MikeOscarEcho Mar 06 '20 at 21:17
  • Yes I did. I follow the way which @Max P gave. which made the bottom sheet as expected but the main activity has become white. – Ariful Haque Mar 07 '20 at 17:30

2 Answers2

2

After trying so many ways I found my solution. Simply added this code in my fragment.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}

BottomSheetDialog with transparent background

Ariful Haque
  • 183
  • 1
  • 9
  • onActivityCreated is deprecated now, so this logic should be implemented inside onViewCreated call. Like: ((this.view?.parent) as View?)?.setBackgroundColor(Color.TRANSPARENT) – Dmitry Smolyaninov Jan 10 '23 at 07:36
1

Your view is actually transparent (when you use transparent instead of black obviously), problem is that BottomSheetDialogFragment wraps your view into it's own non-transparent container. Here's how to make that container transparent: https://stackoverflow.com/a/46469709/12976196

Max P
  • 21
  • 4
  • It becomes full white background above the main page where it should be like the bottom sheet which is above the main page would be transparent background. – Ariful Haque Mar 07 '20 at 17:27