154

How do you set the state of a fragment extending BottomSheetDialogFragment to expanded using BottomSheetBehavior#setState(STATE_EXPANDED) using the Android Support Design Library (v23.2.1)?

https://code.google.com/p/android/issues/detail?id=202396 says:

Bottom sheets are set to STATE_COLLAPSED at first. Call BottomSheetBehavior#setState(STATE_EXPANDED) if you want to expand it. Note that you cannot call the method before view layouts.

The suggested practice requires a view to be inflated first, but I'm not sure how I'll set the BottomSheetBehaviour onto a fragment (BottomSheetDialogFragment).

View bottomSheet = coordinatorLayout.findViewById(R.id.bottom_sheet);  
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);  
user2560886
  • 1,910
  • 2
  • 14
  • 20

23 Answers23

294

"Note that you cannot call the method before view layouts."

The above text is the clue.

Dialogs have a listener that is fired once the dialog is shown. The dialog cannot be shown if it isn't laid out.

So, in the onCreateDialog() of your modal bottom sheet (BottomSheetFragment), just before returning the dialog (or anywhere, once you have a reference to the dialog), call:

// This listener's onShow is fired when the dialog is shown
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {

        // In a previous life I used this method to get handles to the positive and negative buttons
        // of a dialog in order to change their Typeface. Good ol' days.

        BottomSheetDialog d = (BottomSheetDialog) dialog;
        
        // This is gotten directly from the source of BottomSheetDialog
        // in the wrapInBottomSheet() method
        FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);

        // Right here!
        BottomSheetBehavior.from(bottomSheet)
            .setState(BottomSheetBehavior.STATE_EXPANDED);
    }
});

In my case, my custom BottomSheet turned out to be:

@SuppressWarnings("ConstantConditions")
public class ShareBottomSheetFragment extends AppCompatDialogFragment {

    @NonNull @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        BottomSheetDialog dialog =
                new BottomSheetDialog(getActivity(), R.style.Haute_Dialog_ShareImage);

        dialog.setContentView(R.layout.dialog_share_image);

        dialog.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;

                FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
                BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });

        SwitchCompat switchview = (SwitchCompat) dialog.findViewById(R.id.switchview);
        switchview.setTypeface(FontCache.get(dialog.getContext(), lookup(muli, NORMAL)));

        return dialog;
    }
}

Let me know if this helps.

UPDATE

Note that you can also override BottomSheetDialogFragment as:

public class SimpleInitiallyExpandedBottomSheetFragment extends BottomSheetDialogFragment {

    @NonNull @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;

                FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
                BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });

        // Do something with your dialog like setContentView() or whatever
        return dialog;
    }
}

But I really don't see why anyone would want to do that as the base BottomSheetFragment doesn't do anything other than return a BottomSheetDialog.

UPDATE FOR ANDROIDX

When using AndroidX, the resource previously found at android.support.design.R.id.design_bottom_sheet can now be found at com.google.android.material.R.id.design_bottom_sheet.

Rahul
  • 3,293
  • 2
  • 31
  • 43
efemoney
  • 3,616
  • 1
  • 14
  • 16
  • Thanks, I tried this method. It makes the `BottomSheetDialogFragment` appear janky (appears to skip frames in the opening animation) as it goes from the collapsed to expanded behavior. Edit: Tested this on Android Marshmallow and KitKat devices – user2560886 Mar 25 '16 at 23:54
  • 1
    It works perfectly for me. No skipping. Are you doing anything else apart from just returning a dialog? Will appreciate if you update your post with your code so I can have a better Idea. – efemoney Mar 26 '16 at 00:37
  • @user2560886 this is probably because you have a debugger attached; when running detached from a debugger it will probably run smoothly. – swooby Dec 06 '16 at 19:31
  • 5
    Is it just me getting cannot find package `android.support.design.R` after updating the support libraries? – natario Sep 17 '17 at 12:53
  • This answer seems a bit too difficult. Please see my answer on another post having the same issue: https://stackoverflow.com/a/48169762/555914 – Pepijn Jan 09 '18 at 14:04
  • You saved my day with finding this view: design_bottom_sheet. It was such a pain to achieve rounded corners for the background of the bottom sheet. Good job! – Ultimo_m Jul 26 '18 at 13:04
  • @natario It's probably because of AndroidX – lucas_sales Sep 24 '18 at 12:46
  • 2
    I'm also having issues with resolving `android.support.design.R`, just like @natario . I'm using `implementation "com.google.android.material:material:1.0.0"`. I'm also using AndroidX in the project. – hsson Oct 08 '18 at 09:08
  • 31
    When using AndroidX the resource can be found at `com.google.android.material.R.id.design_bottom_sheet` – urgentx Nov 23 '18 at 00:35
  • "But I really dont see why anyone would want to do that" - fragment shows the dialog for you after config change – arekolek Mar 19 '19 at 10:26
  • The above approach can be used to disable hiding on swipe. – ror Mar 27 '19 at 07:13
  • I use this https://medium.com/@OguzhanAlpayli/bottom-sheet-dialog-fragment-expanded-full-height-65b725c8309 – Ho Luong Nov 14 '19 at 07:11
57

efeturi's answer is great, however, if you want to use onCreateView() to create your BottomSheet, as opposed to going with onCreateDialog(), here is the code you will need to add under your onCreateView() method:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    getDialog().setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;
            View bottomSheetInternal = d.findViewById(android.support.design.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheetInternal).setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });
    return inflater.inflate(R.layout.your_bottomsheet_content_layout, container, false);
}
goodKode
  • 3,090
  • 1
  • 20
  • 14
  • 7
    Alternatively you don't need to call getDialog at all. I find the cleanest way to do it is to override both onCreateView and onCreateDialog. Build your view in onCreateView (as you would with any fragment) and do the dialog specific code in onCreateDialog (call super.onCreateDialog to get the instance) – Stimsoni May 24 '16 at 03:47
  • 2
    This save my day. Thanks. – AndroidRuntimeException Mar 30 '17 at 14:33
  • @Stimsoni onCreateView isn't called if onCreateDialog is used. https://developer.android.com/reference/android/support/v4/app/DialogFragment.html#oncreatedialog – Vincent Paing Mar 11 '19 at 05:35
  • 2
    @Vincent_Paing, Yes it is. In your attached link it says 'onCreateView does not need to be implemented'. It doesn't say it won't be called. Take a look at the source code here https://github.com/material-components/material-components-android/blob/master/lib/java/com/google/android/material/bottomsheet/BottomSheetDialogFragment.java. The default implementation calls onCreateDialog to create the bottom sheet and every solution above is still using onCreateView meaning they are both always called. Just make sure you still call super.onCreateDialog() if you do override it. – Stimsoni Mar 12 '19 at 05:13
  • in BottomSheetDialogFragment it crash me in onCreateView() I put it in onViewCreated() and it's perfect! thank you – avisper Sep 07 '20 at 10:23
29

A simplistic and elegant solution:

BottomSheetDialogFragment could be subclassed to address this:

class NonCollapsableBottomSheetDialogFragment extends BottomSheetDialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final BottomSheetDialog dialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                FrameLayout bottomSheet = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);

                BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
                behavior.setSkipCollapsed(true);
                behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });
        return dialog;
    }
}

So extend this class instead of BottomSheetDialogFragment to create your own bottom sheet.

Note

Change com.google.android.material.R.id.design_bottom_sheet to android.support.design.R.id.design_bottom_sheet if your project uses old Android support libraries.

DYS
  • 2,826
  • 2
  • 23
  • 34
25

BottomSheetDialogFragment:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    (dialog as? BottomSheetDialog)?.behavior?.state = STATE_EXPANDED
}

or when ready to show:

private fun onContentLoaded(items: List<Any>) {
    adapter.submitList(items)
    (dialog as? BottomSheetDialog)?.behavior?.state = STATE_EXPANDED
}
Yeldar Nurpeissov
  • 1,786
  • 14
  • 19
  • 3
    This solution worked perfectly for me. It's a one-liner and much simpler than everything else before. Not sure why it has not gotten more upvotes... – AlexB Aug 26 '22 at 23:03
12

I think those above is better. Sadly I did not find those solution before I had solved. But write my solution. quite similar to all.

==================================================================================

I face the same issue. This is what I solved. The Behavior is hidden in BottomSheetDialog, which is available to get the behavior If you would like not to change your parent layout to be CooridateLayout, you can try this.

STEP 1: customize the BottomSheetDialogFragment

open class CBottomSheetDialogFragment : BottomSheetDialogFragment() {
   //wanna get the bottomSheetDialog
   protected lateinit var dialog : BottomSheetDialog 
   override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
      dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
      return dialog
   }

   //set the behavior here
   fun setFullScreen(){
      dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
   }
}

STEP 2: make your fragment extend this customized fragment

class YourBottomSheetFragment : CBottomSheetDialogFragment(){
    
   //make sure invoke this method after view is built
   //such as after OnActivityCreated(savedInstanceState: Bundle?)
   override fun onStart() {
      super.onStart()
      setFullScreen()//initiated at onActivityCreated(), onStart()
   }
}
lasec0203
  • 2,422
  • 1
  • 21
  • 36
Tina Lu
  • 363
  • 3
  • 10
11

In Kotlin, add the below line in onStart() of your BottomSheetDialogFragment

(dialog as BottomSheetDialog).behavior.state = BottomSheetBehavior.STATE_EXPANDED
Ajay Mourya
  • 220
  • 4
  • 6
5

My answer is more or less same as most of the above answers with a slight modification. Instead of using findViewById to first find the bottom sheet view, I have preferred not to hardcode any framework view resource ids since they might change in future.

setOnShowListener(dialog -> {
            BottomSheetBehavior bottomSheetBehavior = ((BottomSheetDialog)dialog).getBehavior();
            bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        });
prateek
  • 440
  • 4
  • 12
5
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return super.onCreateDialog(savedInstanceState).apply {
        setOnShowListener {
            (this@TipsBottomDialogFragment.dialog as BottomSheetDialog).behavior.setState(
                BottomSheetBehavior.STATE_EXPANDED
            )
        }
    }
}
leigo
  • 261
  • 3
  • 3
5

Posting this here to future readers, as I think now we can use another solution.

I was trying to solve the same problem you described with a BottomSheetDialog.

I don't like using internal Android ids and I've just found there's a method inside BottomSheetDialog getBehavior that you can use:

You can use this inside your BottomSheetDialog:

behavior.state = BottomSheetBehavior.STATE_EXPANDED

Using BottomSheetDialogFragment you can do the same casting the dialog from that DialogFragment to BottomSheetDialog.

culebrins
  • 428
  • 9
  • 21
5

You can do the following (Kotlin version):

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.let {
        val sheet = it as BottomSheetDialog
        sheet.behavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

    // rest of your stuff
}
artenson.art98
  • 1,543
  • 15
  • 15
5

Here is a pretty neat Kotlin solution that works great.

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return (super.onCreateDialog(savedInstanceState) as BottomSheetDialog).apply {
        setOnShowListener {
           behavior.state = BottomSheetBehavior.STATE_EXPANDED
        }
    }
}
Jan Kotas
  • 71
  • 1
  • 3
4
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;

                FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet);
                BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });

I met NullPointException in BottomSheetBehavior.from(bottomSheet) because d.findViewById(android.support.design.R.id.design_bottom_sheet) returns null.

It's strange. I add this line of code to Watches in Android Monitor in DEBUG mode and found it return Framelayout normally.

Here's code of wrapInBottomSheet in BottomSheetDialog:

private View wrapInBottomSheet(int layoutResId, View view, ViewGroup.LayoutParams params) {
        final CoordinatorLayout coordinator = (CoordinatorLayout) View.inflate(getContext(),
                R.layout.design_bottom_sheet_dialog, null);
        if (layoutResId != 0 && view == null) {
            view = getLayoutInflater().inflate(layoutResId, coordinator, false);
        }
        FrameLayout bottomSheet = (FrameLayout) coordinator.findViewById(R.id.design_bottom_sheet);
        BottomSheetBehavior.from(bottomSheet).setBottomSheetCallback(mBottomSheetCallback);
        if (params == null) {
            bottomSheet.addView(view);
        } else {
            bottomSheet.addView(view, params);
        }
        // We treat the CoordinatorLayout as outside the dialog though it is technically inside
        if (shouldWindowCloseOnTouchOutside()) {
            coordinator.findViewById(R.id.touch_outside).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (isShowing()) {
                                cancel();
                            }
                        }
                    });
        }
        return coordinator;
    }

Occasionally, I found that R.id.design_bottom_sheet is not equal to android.support.design.R.id.design_bottom_sheet. They have different value in different R.java.

So I change android.support.design.R.id.design_bottom_sheet to R.id.design_bottom_sheet.

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                BottomSheetDialog d = (BottomSheetDialog) dialog;

                FrameLayout bottomSheet = (FrameLayout) d.findViewById(R.id.design_bottom_sheet); // use R.java of current project
                BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });

No more NullPointException now.

legendmohe
  • 750
  • 7
  • 11
4

All results with using onShow() cause random render bug when soft keyboard is displayed. See screenshot bellow - BottomSheet dialog is not at bottom of the screen but is placed like the keyboard was displayed. This problem does not occur always but quite often.

enter image description here

UPDATE

My solution with the reflection of the private members is unnecessary. Using postDelayed (with about 100 ms) for creating and showing dialog after hiding the soft keyboard is a better solution. Then the above solutions with onShow() are ok.

Utils.hideSoftKeyboard(this);
mView.postDelayed(new Runnable() {
    @Override
    public void run() {
        MyBottomSheetDialog dialog = new MyBottomSheetDialog();
        dialog.setListener(MyActivity.this);
        dialog.show(getSupportFragmentManager(), TAG_BOTTOM_SHEET_DLG);
    }
}, 100);

So I implement another solution, but it requires using reflection because BottomSheetDialog has all members as private. But it solves render bugs. BottomSheetDialogFragment class is only AppCompatDialogFragment with onCreateDialog method which create BottomSheetDialog. I create own child of AppCompatDialogFragment which create my class extends BottomSheetDialog and which solves access to private behavior member and set it in onStart method to STATE_EXPANDED state.

public class ExpandedBottomSheetDialog extends BottomSheetDialog {

    protected BottomSheetBehavior<FrameLayout> mBehavior;

    public ExpandedBottomSheetDialog(@NonNull Context context, @StyleRes int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            Field privateField = BottomSheetDialog.class.getDeclaredField("mBehavior");
            privateField.setAccessible(true);
            mBehavior = (BottomSheetBehavior<FrameLayout>) privateField.get(this);
        } catch (NoSuchFieldException e) {
            // do nothing
        } catch (IllegalAccessException e) {
            // do nothing
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mBehavior != null) {
            mBehavior.setSkipCollapsed(true);
            mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }
}


public class AddAttachmentBottomSheetDialog extends AppCompatDialogFragment {

    ....

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new ExpandedBottomSheetDialog(getContext(), getTheme());
    }

    ....
}
Rahul
  • 3,293
  • 2
  • 31
  • 43
Petr Daňa
  • 1,263
  • 12
  • 18
4

In your Kotlin BottomSheetDialogFragment class, override onCreateDialog as below

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val bottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        bottomSheetDialog.setOnShowListener {
            val bottomSheet =
                bottomSheetDialog.findViewById<FrameLayout>(
                    com.google.android.material.R.id.design_bottom_sheet
                )
            val behavior = BottomSheetBehavior.from(bottomSheet!!)
            behavior.skipCollapsed = true
            behavior.state = BottomSheetBehavior.STATE_EXPANDED
        }
        return bottomSheetDialog
    }
Emad Razavi
  • 1,903
  • 2
  • 17
  • 24
3

Apply BottomsheetDialogFragment state in onResume will solve this issue

@Override
public void onResume() {
    super.onResume();
    if(mBehavior!=null)
       mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}

onShow(DialogInterface dialog) and postDelayed may cause animation glitch

John Ruban Singh
  • 1,284
  • 14
  • 19
3

Similar to uregentx answer, in kotlin, you can declare your fragment class that extends from BottomSheetDialogFragment, and when the view is created you can set the dialog listener default state after the dialog is displayed.

STATE_COLLAPSED: The bottom sheet is visible but only showing its peek height.

STATE_EXPANDED: The bottom sheet is visible and its maximum height.

STATE_HALF_EXPANDED: The bottom sheet is visible but only showing its half height.

class FragmentCreateGroup : BottomSheetDialogFragment() {
      ...

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
        // Set dialog initial state when shown
        dialog?.setOnShowListener {
            val bottomSheetDialog = it as BottomSheetDialog
            val sheetInternal: View = bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet)!!
            BottomSheetBehavior.from(sheetInternal).state = BottomSheetBehavior.STATE_COLLAPSED
        }

        val view = inflater.inflate(R.layout.fragment_create_group, container, false)
        ...

        return view
    }
}

Remember using material design implementation in gradle.

implementation "com.google.android.material:material:$version"

Also take a look to material design reference Bottom Sheets

Rahul Gaur
  • 1,661
  • 1
  • 13
  • 29
FJCG
  • 1,017
  • 7
  • 6
  • Where does the `dialog?` variable come from in onCreateView? – Eric Smith Nov 09 '19 at 14:50
  • `dialog` is a property of the class `DialogFragment`, actually is a Getter. In this example I used that getter to get the current DialogFragment instance and `setOnShowListener` to it. May be you have already used that kind of instructions in your project, for example in a activity, to access to action bar `actionBar` getter is used, so you can modify that component, for example `actionBar?.subtitle = "abcd"` – FJCG Nov 09 '19 at 20:01
2

Simple Solution to expand the BottomSheet view in Kotlin:

(dialog as BottomSheetDialog).behavior.state = 
    BottomSheetBehavior.STATE_EXPANDED
Ali Sidhu
  • 119
  • 9
2
val bottomSheetDialog = BottomSheetDialog(context, R.style.BottomSheetDialogTheme)
bottomSheetDialog.setCanceledOnTouchOutside(true)
bottomSheetDialog.setContentView(view)
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED

This is solve your problem.

1

The easiest way I implemented is as below, Here we are finding android.support.design.R.id.design_bottom_sheet and setting bottom sheet state as EXPANDED.

Without this, my bottom sheet was always stuck in the COLLAPSED state if view height is more than 0.5 of screen height and I have to manually scroll to view full bottom sheet.

class BottomSheetDialogExpanded(context: Context) : BottomSheetDialog(context) {

    private lateinit var mBehavior: BottomSheetBehavior<FrameLayout>

    override fun setContentView(view: View) {
        super.setContentView(view)
        val bottomSheet = window.decorView.findViewById<View>(android.support.design.R.id.design_bottom_sheet) as FrameLayout
        mBehavior = BottomSheetBehavior.from(bottomSheet)
        mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
    }

    override fun onStart() {
        super.onStart()
        mBehavior.state = BottomSheetBehavior.STATE_EXPANDED
    }
}
Akhil
  • 6,667
  • 4
  • 31
  • 61
0
@NonNull @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    BottomSheetDialog dialog =
            new BottomSheetDialog(getActivity());

    dialog.setContentView(R.layout.bottomsheet_no_region);
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            BottomSheetDialog d = (BottomSheetDialog) dialog;

            FrameLayout bottomSheet = (FrameLayout) d.findViewById(com.google.android.material.R.id.design_bottom_sheet);
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    });
    return dialog;
}
shubomb
  • 672
  • 7
  • 20
0

The better solution is to override the method To make it expanded and transparent

   @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    BottomSheetDialog dialog = new BottomSheetDialog(getContext(),R.style.MyTransparentBottomSheetDialogTheme);
    dialog.getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
    return dialog;
}
Rohaitas Tanoli
  • 624
  • 4
  • 16
-1

That's what worked for me, based on the response from the link below.

behavior = BottomSheetBehavior.from(bottomSheet1);
if(action.equals("post") ) {
    behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    behavior.setDraggable(false); // disable dragging
}

enter link description here

Dominique
  • 16,450
  • 15
  • 56
  • 112
-1

Simple answer (Kotlin + fragment + bottomSheetDialogViewBinding) :

val bsd = BottomSheetDialog(requireContext())
val bsdBinding = DialogBottomSheetViewBinding.inflate(LayoutInflater.from(requireContext()))
bsd.behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
pravingaikwad07
  • 482
  • 1
  • 7
  • 24