0

I have a BottomSheetDialog class that shows when we click the button, I need to make it full screen not on half on the page.

public class BottomSheetDialogBuyPlan extends BottomSheetDialog {
       public BottomSheetDialogBuyPlan(@NonNull Context context) {
    super(context);


    BottomSheetBehavior<FrameLayout> behavior = getBehavior();
    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    View bottomSheet = getLayoutInflater().inflate(R.layout.layout, null);
    setContentView(bottomSheet);
    show();
  }

    @Override
    public void setOnShowListener(@Nullable OnShowListener listener) {
        super.setOnShowListener(listener);
    }
}

this is how i call it in activity

    BottomSheetDialogBuyPlan bottomSheetDialog = new 
   BottomSheetDialogBuyPlan(getContext());

How to make it full screen?

Malo
  • 1,232
  • 2
  • 17
  • 28

1 Answers1

0

Your code shown here is so limited that I can't also present code that is directly applicable to your case. At least I would suggest there is BottomSheetBehavior#setState(BottomSheetBehavior.STATE_EXPANDED) method.

Here is a minimum sample code:

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BottomSheetDialog dialog = new BottomSheetDialog(this);
        BottomSheetBehavior<FrameLayout> behavior = dialog.getBehavior();
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        View bottomSheet = getLayoutInflater().inflate(R.layout.bottom_sheet, null);
        dialog.setContentView(bottomSheet);
        dialog.show();
    }
}

layout/bottom_sheet:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="a\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na"
        android:textSize="30sp" />

</ScrollView>

EDIT:

You can manage to expand the BottomSheet's content layout beyond its content originally requiring though I doubt you need to use BottomSheet for such usage...

MyBottomSheetDialog:

public class MyBottomSheetDialog extends BottomSheetDialog {
    public MyBottomSheetDialog(@NonNull Context context) {
        super(context);

        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager()
                .getDefaultDisplay()
                .getMetrics(displayMetrics);

        BottomSheetBehavior<FrameLayout> behavior = getBehavior();
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        View bottomSheet = getLayoutInflater().inflate(R.layout.bottom_sheet, null);
        bottomSheet.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, displayMetrics.heightPixels
        ));
        setContentView(bottomSheet);
    }
}

layout/bottom_sheet:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="a\na"
        android:textSize="30sp" />
</FrameLayout>
hata
  • 11,633
  • 6
  • 46
  • 69
  • not working in all cases i put this code before show() function and not working – Malo Nov 06 '21 at 14:12
  • @Malo You didn't disclose `show()` method. I can't tell anything. – hata Nov 06 '21 at 14:21
  • check my question updated – Malo Nov 06 '21 at 14:23
  • @Malo `show()` should be used on the `bottomSheetDialog` in your Activity (not in `BottomSheetDialogBuyPlan`) after it was instantiated. – hata Nov 06 '21 at 14:31
  • ok it worked just if the text have 30 'a' letters so the page expands vertically with textview..else it show half of the page – Malo Nov 06 '21 at 14:33
  • @Malo it shows all of the content of the sheet not half of that. If it has two lines, it will show all of two lines not only one line. It has already been fully expanded. Thus is BottomSheet. – hata Nov 06 '21 at 15:10