Bottom Sheet dialog landscape width issue

Viewed 668

bottom_sheet_image_dialog.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="62dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/llAddDriverPic"
    android:background="?android:attr/selectableItemBackground"
    android:padding="8dp">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/baseline_add_photo_alternate_24"
        android:layout_margin="8dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/add_picture"
        android:layout_gravity="center_vertical"
        android:padding="8dp"/>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/llRemoveDriverPic"
    android:background="?android:attr/selectableItemBackground"
    android:padding="8dp">

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/baseline_no_photography_24"
        android:layout_margin="8dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/remove_picture"
        android:layout_gravity="center_vertical"
        android:padding="8dp"/>

</LinearLayout>

Code implementation:

BottomSheetDialog bsDialog = new BottomSheetDialog(getContext());
bsDialog.setContentView(R.layout.bottom_sheet_image_dialog);

bsDialog.setOnShowListener(dialog -> {
    BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior();
    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
});

Following issue which occurs: The width, as seen in the video won't appear fully. If I start the bottomsheet in the beginning in landscape mode the width also looks different as the left and right side are not covered:

exampleLendscape

What may the issue(s) be? Is it necessary to predefine the width before showing the dialog?

1 Answers

-> There's no problem with your xml layout, It can be solved in Code. I am providing an example below which solved this issue.

=> Bottom Sheet Class Code

public class BottomSheet extends BottomSheetDialogFragment {

private View view;
private BottomSheetBehavior mBottomBehavior;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.bottom_sheet_layout, container, false);

  //Do your logic code here

    return view;
}

@Override
public void onStart() {
    super.onStart();
    mBottomBehavior = BottomSheetBehavior.from((View) view.getParent());
    mBottomBehavior.setMaxWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    mBottomBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

}

=> Activity Class Code

Button button = findViewById(R.id.button);

button.setOnClickListener(v -> {

//To open the bottom sheet, NB. make sure 'BottomSheet' is the name you declared Bottom sheet class

    BottomSheet bottomSheet = new BottomSheet();
    bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
});
Related