Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android bottom sheet layout margin

Tags:

android

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>
like image 328
Ali Zarei Avatar asked Jun 05 '16 08:06

Ali Zarei


People also ask

How do I stop my android from dragging the bottom sheet?

Disable drag of BottomSheetDialogFragment Even if we have made the BottomSheetDialogFragment as expanded, user can hold the top part of the BottomSheet and drag to show peek view. It can also be disabled by overriding onStateChanged() . This will set the expanded state even if user drags the view.


2 Answers

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)
like image 141
Mahdi nezam parast Avatar answered Oct 16 '22 04:10

Mahdi nezam parast


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>

like image 28
Adam Katz Avatar answered Oct 16 '22 05:10

Adam Katz