Search code examples
androidandroid-relativelayoutdefault-value

Android | Java | How to remove default margins from relative layout?


I have a bottomsheet which includes a layout. And that layout has relative layout. Which has default margins. Even i set margins to 0dp it's not working.

bottom_sheet_back.xml

<?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="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/blue_back"/>

</RelativeLayout>

code inside activity_main.xml


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:elevation="10dp"
        android:padding="0dp">

        <com.google.android.material.bottomappbar.BottomAppBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom">

            <include layout="@layout/bottom_sheet_back"/>

        </com.google.android.material.bottomappbar.BottomAppBar>

This is giving me result like this.

result

where as bottom_sheet_back.xml has full width.

bottom_sheet_bacck

As you can see in 1st image i want to remove that margins.

Solution Tried

  1. Android UI Default Margin Remove
  2. Android Remove default margins in RelativeLayout

Above solutions are not much help in my scenario. I tried one more solution and it was to edit dimens.xml but, I can't seem to find it anywhere. I use android studio 4.1.3 which has constraintLayout by default. So, dimens.xml is not generated by project. But, if it is not generated by project then how did RelativeLayout got margins?

Any help will be appreciated.


Solution

  • I have managed to get around your problem, i think theres aproblem with BottomAppbar, but if u change to BottomNavigationView it works very well with no problem, it removes the default margin

    here is the code for the Bottom navigation i used

    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom">
    
        <include layout="@layout/bottom_sheet_back"/>
    
    </com.google.android.material.bottomnavigation.BottomNavigationView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    thanks