4

I've tried adding margin to my BottomSheetDialogFragment, however it doesn't do anything for the margins.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="16dp">

    <TextView
        android:id="@+id/alertdialog_fragmail_newmessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 1"
        android:textStyle="bold"
        android:padding="16dp"
        android:textColor="@color/colorBlackFont"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:drawablePadding="16dp"/>


  //More Textviews

</RelativeLayout>

Edit:_________________________________________________________________

I've tried changing the XML to the answer below, however it's still not creating margins for my bottomsheetdialogfragment.

The code for the Bottom sheet dialog fragment class:

public class FragMailMoreDialog extends BottomSheetDialogFragment {

    private static final String TAG = "FragMailMoreDialog";

    private Context mContext;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

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


        return view;
    }
}

The code to inflating the bottomsheet:

private void inflateMoreDialog(){
        FragMailMoreDialog moreDialog = new FragMailMoreDialog();
        if (getFragmentManager() != null) {
            moreDialog.show(getFragmentManager(), "FRAGMAIL_MORE_DIALOG");
        }
    }
DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
  • Attach full code please – MMG Mar 14 '20 at 04:30
  • 1
    Are you expecting margins on all sides, kinda like a floating window? Or just on the left and right sides? [This answer](https://stackoverflow.com/a/54683903) will do the left and right, but you might have to update the theme and style `parent`s – if you're using a newer library – and you'd have to override `onCreateDialog()` in your `ButtomSheetFragment` to return a `BottomSheetDialog` constructed like is shown there. No `setContentView()` or `show()` call, though. – Mike M. Mar 14 '20 at 06:49
  • Yeah I'm trying to get margins on all sides like a floating window – DIRTY DAVE Mar 14 '20 at 07:44
  • Ended up doing this https://stackoverflow.com/a/55219784/11110509 – DIRTY DAVE Mar 14 '20 at 08:06

7 Answers7

5

A bit of a hacky solution:

I wrapped my layout in another RelativeLayout and made the background of that layout transparent.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        android:layout_margin="16dp">

    <TextView
        android:id="@+id/alertdialog_fragmail_newmessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 1"
        android:textStyle="bold"
        android:padding="16dp"
        android:textColor="@color/colorBlackFont"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:drawablePadding="16dp"
        android:background="?attr/selectableItemBackground"/>
    </RelativeLayout>
</RelativeLayout>

And in the BottomSheetDialogFragment you need to override setupDialog

@Override
    public void setupDialog(Dialog dialog, int style) {
        View contentView = View.inflate(getContext(), R.layout.alertdialog_layout_fragmailmore, null);
        dialog.setContentView(contentView);
        ((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }

Credits go to this person:

https://stackoverflow.com/a/55219784/11110509

DIRTY DAVE
  • 2,523
  • 2
  • 20
  • 83
5

You can do it in stiles.xml. Please note that for a bottom_margin to work, you actually need to add a negative marginTop (see -10dp), which will shift everything up by 10dp.

<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:backgroundDimAmount">0.5</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
</style>

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/bottom_menu_back</item>
    <item name="android:layout_marginStart">10dp</item>
    <item name="android:layout_marginEnd">10dp</item>
    <item name="android:layout_marginTop">-10dp</item>
</style>
ekashking
  • 387
  • 6
  • 19
2

Overriding setupDialog() didn't work for me, and also I need to use onCreateView() for inflating the dialog layout. So, this can be solved programmatically in the customized BottomSheetDialogFragment class:

  • Create a no background style & set it for the dialog either in onCreateDialog or getTheme()
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:windowBackground">@null</item>
</style>
  • Set the margin using the layoutParams of the root of the dialog layout

Java:

public class FragMailMoreDialog extends BottomSheetDialogFragment {

    //.... Omitted code

        
    @Override
    public int getTheme() {
       // Step 1
       return R.style.NoBackgroundDialogTheme;
    }


    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        // Step 2
        addMargin(view);

    }

    private void addMargin(View view) {
        FrameLayout.LayoutParams layoutParams =
                (FrameLayout.LayoutParams) view.getLayoutParams();
        int margin_16dp = dpToPx(16);
        layoutParams.setMargins(margin_16dp, margin_16dp, margin_16dp, margin_16dp);
        view.setLayoutParams(layoutParams);
        view.requestLayout();
    }

    private int dpToPx(int dp) {
        Resources r = getResources();
        int px = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                r.getDisplayMetrics()
        );
        return px;
    }

}

Kotlin:

class FragMailMoreDialog() : BottomSheetDialogFragment() {

    override fun getTheme(): Int {
        return R.style.NoBackgroundDialogTheme
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        val view: View = View.inflate(context, R.layout.my_fragment_bottomsheet, null)
        return view
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        addMargin(view)
    }

    private fun addMargin(view: View) {
        val layoutParams: FrameLayout.LayoutParams =
            view.layoutParams as FrameLayout.LayoutParams
        val margin_16dp = 16.toPx().toInt()
        layoutParams.setMargins(margin_16dp, margin_16dp, margin_16dp, margin_16dp)

        view.layoutParams = layoutParams
    }


    fun Number.toPx() = TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        this.toFloat(),
        Resources.getSystem().displayMetrics
    )

}
Zain
  • 37,492
  • 7
  • 60
  • 84
0

Not sure why you want a margin for the BottomSheetDialogFragment. It is a DialogFragment showing on top of your Activity/Fragment. Adding a margin to it will not do anything. If what you need are the top padding of TextView (Test1) and bottom padding of TextView (Test2) then you should add padding_top to Test1 and padding_bottom to Test2 like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="16dp">

    <TextView
        android:id="@+id/alertdialog_fragmail_newmessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 1"
        android:textStyle="bold"
        android:paddingTop="32dp"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        android:paddingBottom="16dp"
        android:textColor="@color/colorBlackFont"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:drawablePadding="16dp"/>
</RelativeLayout>
Alan Van
  • 81
  • 2
0

I Added margin to the Constraint child and give parent Constraint background transparent Bottom Sheet layout Image

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#99000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        style="@style/BottomSheetDialogStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/margin_medium"
        android:layout_marginEnd="@dimen/margin_medium"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/iv_add_post_ic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_medium"
            android:layout_marginTop="@dimen/margin_form"
            android:layout_marginBottom="@dimen/margin_small"
            android:src="@drawable/ic_add_post"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tv_add_post"
            style="@style/Widget.nejmo.Text.Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_form"
            android:layout_marginHorizontal="@dimen/margin_small"
            android:layout_marginStart="@dimen/margin_small"
            android:layout_marginBottom="@dimen/margin_small"
            android:text="@string/option_add_post_txt"
            android:textColor="@color/dusk"
            app:layout_constraintBottom_toBottomOf="@+id/iv_add_post_ic"
            app:layout_constraintStart_toEndOf="@+id/iv_add_post_ic"
            app:layout_constraintTop_toTopOf="@+id/iv_add_post_ic" />


        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/iv_add_question_ic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_medium"
            android:layout_marginTop="@dimen/margin_medium"
            android:layout_marginBottom="@dimen/margin_medium"
            android:src="@drawable/ic_add_question"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/iv_add_post_ic" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tv_add_question"
            style="@style/Widget.nejmo.Text.Medium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_form"
            android:layout_marginHorizontal="@dimen/margin_small"
            android:layout_marginStart="@dimen/margin_small"
            android:layout_marginTop="@dimen/margin_medium"
            android:layout_marginBottom="@dimen/margin_medium"
            android:text="@string/option_add_question_txt"
            android:textColor="@color/dusk"
            app:layout_constraintBottom_toBottomOf="@+id/iv_add_question_ic"
            app:layout_constraintStart_toEndOf="@+id/iv_add_question_ic"
            app:layout_constraintTop_toTopOf="@+id/iv_add_question_ic" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
0

works 100%

Override onCreateDialog() return your own dialog

   override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
  
        return Dialog(requireActivity()).apply {
            setContentView(viewBinding.root)
            window!!.setBackgroundDrawableResource(R.drawable.dialog_background)
            window!!.setGravity(Gravity.BOTTOM)
            window!!.setLayout(500.toPx(),500.toPx())
        }
  
}
Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
0

Similarly to @Zain answer:

override fun onCreateView(...) {
    ...
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // FrameLayout.LayoutParams, CoordinatorLayout.LayoutParams or other container in your root layout.
    ((view.parent as View).layoutParams as FrameLayout.LayoutParams).topMargin = 100
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224