androidandroid-bottomsheetdialog

Adding margin to BottomSheetDialogFragment


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");
        }
    }

Solution

  • 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