BekaKK
BekaKK

Reputation: 2253

Change PeekHeight in BottomSheetDialogFragment

I'm working BottomSheetDialogFragment.Everything is perfect,but i have one problem.I can't change PeekHeight in my BottomSheetDialogFragment.This is my source

public class BottomSheet3DialogFragment extends BottomSheetDialogFragment {

private BottomSheetBehavior mBottomSheetBehavior2;
private BottomSheetBehavior.BottomSheetCallback
        mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {
    @Override
    public void onStateChanged(@NonNull View bottomSheet, int newState) {
        if (newState == BottomSheetBehavior.STATE_HIDDEN) {
            dismiss();
        }
        mBottomSheetBehavior2= BottomSheetBehavior.from(bottomSheet);
        if(mBottomSheetBehavior2!=null)
            mBottomSheetBehavior2.setPeekHeight(20);


    }

    @Override
    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
    }
};

@Override
public void setupDialog(final Dialog dialog, int style) {
    super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.fragment_bottomsheet3, null);
    dialog.setContentView(contentView);
}

}

mButton3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BottomSheet3DialogFragment bottomSheetDialogFragment = new BottomSheet3DialogFragment();

            bottomSheetDialogFragment.show(getSupportFragmentManager(), bottomSheetDialogFragment.getTag());
        }
    });

Is it a possible to change PeekHeight in my Fragment? If anyone knows solution please help me thanks

Upvotes: 2

Views: 1516

Answers (2)

Zeinab Rahnamaee
Zeinab Rahnamaee

Reputation: 53

I had same problem and this worked for me!

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            bottomSheet = dialog.findViewById(R.id.design_bottom_sheet);
            bottomSheet.getLayoutParams().height = 200;
        }
        final View view = getView();
        view.post(new Runnable() {
            @Override
            public void run() {
                View parent = (View) view.getParent();
                CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) (parent).getLayoutParams();
                CoordinatorLayout.Behavior behavior = params.getBehavior();
                BottomSheetBehavior bottomSheetBehavior = (BottomSheetBehavior) behavior;
                bottomSheetBehavior.setPeekHeight(view.getMeasuredHeight());
                ((View)bottomSheet.getParent()).setBackgroundColor(Color.TRANSPARENT);
            }
        });
    }

and your bottomsheet layout must be like this : coordinatorlayout

<androidx.coordinatorlayout.widget.CoordinatorLayout
    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="90dp"
    android:background="@color/gray">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <ImageView
            android:id="@+id/img_edit"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginRight="30dp"
            app:srcCompat="@drawable/icon_edit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

        <ImageView
            android:id="@+id/img_delete"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginLeft="25dp"
            app:srcCompat="@drawable/delete_icon"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Upvotes: 1

Michał Janowicz
Michał Janowicz

Reputation: 450

After dialog.setContentView(contentView); you have to put this:

dialog.setOnShowListener(new OnShowListener() {
  @Override
  public void onShow(DialogInterface dialog) {
    FrameLayout bottomSheet = dialog.getWindow()
        .findViewById(android.support.design.R.id.design_bottom_sheet);
    CoordinatorLayout coordinatorLayout = (CoordinatorLayout) bottomSheet.getParent();
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    bottomSheetBehavior.setPeekHeight(bottomSheet.getHeight());
    coordinatorLayout.getParent().requestLayout();
  }
});

This code automatically adjust height to view height. If You want fixed height just change bottomSheet.getHeight() to whatever You want.

Upvotes: 0

Related Questions