'Android remove strange distance between bottomnavview and ad

Here is how it looks like

enter image description here

I want to remove the distance between bottom of the ad and top of the bottom navigation view. I have no idea where is this gap comming from.

Here is my code:

MainActivity.java

private void initFragment() {
    bottomNavigationView.post(() -> {
        bottomNavViewHeight = bottomNavigationView.getMeasuredHeight();
        FragmentList fragmentOne = new FragmentList();
        Bundle args = new Bundle();
        args.putInt("b_nav_height", bottomNavViewHeight);
        fragmentOne.setArguments(args);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragmentOne);
        ft.commit();
    });
}

FragmentList.java

private void initAd() {
    AdView mAdView = getView().findViewById(R.id.adView);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mAdView.getLayoutParams();
    Bundle bundle = getArguments();
    if (bundle != null) {
        numberOfCols = bundle.getString("number_of_cols", "null");
        bottomNavHeight = bundle.getInt("b_nav_height", 0);
    }
    params.setMargins(0, 0, 0, bottomNavHeight); //substitute parameters for left, top, right, bottom
    mAdView.setLayoutParams(params);
    AdRequest adRequest = new AdRequest.Builder()
            .build();
    mAdView.loadAd(adRequest);
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:animateLayoutChanges="true">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="?attr/myBackgroundColor"
            app:menu="@menu/bottom_navigation_items"
            app:labelVisibilityMode="selected"
            app:itemTextAppearanceActive="@color/colorAccent"
            app:itemTextColor="@color/bottom_nav_color"
            app:itemIconTint="@color/bottom_nav_color"/>

    </FrameLayout>
    </androidx.constraintlayout.widget.ConstraintLayout >

fragment_list.xml

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

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/adView">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/card_view_recycler_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/adView" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111" />
</RelativeLayout>

If you need something more just ask. If you need something more just ask. If you need something more just ask.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source