12

how to set margin to bottom sheet in a fragment ?

i have a linear layout at bottom of fragment and now i want to expand bottom sheet from top of that linear layout

android:layout_marginBottom="36dp" not works and bottom sheet covers linear layout.

here is my code :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:background="@drawable/shadow"
    android:layout_gravity="bottom">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_add_white_24dp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000"
    app:behavior_hideable="true"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:layout_marginBottom="36dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test" />

</LinearLayout>
Ali Zarei
  • 3,523
  • 5
  • 33
  • 44

4 Answers4

4

I had the same problem its a bit of a hack but i got it to work like this.

Essentially just made a transparent linear layout with bottom sheet, and then put a view inside it. Then gave the transparent linear layout padding bottom which worked.

<android.support.design.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="match_parent"
android:fitsSystemWindows="true"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:transitionName="vatom"
        />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
    android:elevation="300dp"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    >

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        />


</LinearLayout>

Adam Katz
  • 6,999
  • 11
  • 42
  • 74
  • Thanks. This worked for me. I tried this when I was using a NestedScrollView as a container with the padding, but it didn't work correctly because scroll view's match_parent doesn't actually fill the entire screen. Changing it to a Linear Layout worked for me. – fobbymaster Apr 19 '17 at 18:41
3

I don't know for bottom margin but for horizontal (start or end) margin, the following solution solved my problem:

First write this code in themes.xml (previously known as style.xml) :

<style name="bottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/bottomSheetDialogStyle</item>
</style>

<style name="bottomSheetDialogStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:layout_marginStart">8dp</item>
    <item name="android:layout_marginEnd">8dp</item>
</style>

Then set the style when showing the bottom sheet :

DisplayOrderBottomSheet().apply {

    setStyle(DialogFragment.STYLE_NORMAL, R.style.bottomSheetDialog)

}.show(childFragmentManager)
1

Try using a FrameLayout to hold elements inside the root LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/paymentbottomsheet_view"
android:orientation="vertical"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="215dp"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<FrameLayout
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
//Host another linear layout here to hold your elements.
</FrameLayout>
</LinearLayout>
Uchenna Nnodim
  • 458
  • 4
  • 11
0

You can use this code to set margin to your bottomsheet layout:

You can change only the 0.9 on this line lp.width = (displayMetrics.widthPixels * 0.9); to change your margin size.

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(bottomSheetDialog.getWindow().getAttributes());

        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        lp.width = (displayMetrics.widthPixels * 0.9);
        bottomSheetDialog.getWindow().setAttributes(lp);
    }

Hope this is helpful.

Amit Verma
  • 8,660
  • 8
  • 35
  • 40