Here is what I want.
The last item of recyclerview should be always above ad. Now it is partially hidden by an ad. App consists of main_layout where is FrameLayout with the name content_frame
where fragment lies. Here is what I have written in code:
activity_main.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">
<com.google.android.material.appbar.AppBarLayout
android:id="@ id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@ id/content_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_bar_layout"
app:layout_constraintBottom_toTopOf="@id/bottomNavigation"
android:animateLayoutChanges="true"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorBottomNavigationViewBg"
app:itemIconTint="?attr/colorBottomNavigationViewItem"
app:itemTextAppearanceActive="@color/colorAccent"
app:itemTextColor="?attr/colorBottomNavigationViewItem"
app:labelVisibilityMode="selected"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_navigation_items" />
</androidx.constraintlayout.widget.ConstraintLayout >
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/rl"
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">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</RelativeLayout>
FragmentList.java
private void initAds(View view) {
RelativeLayout relativeLayout = view.findViewById(R.id.rl);
AdView adView = new AdView(view.getContext());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.BELOW, view.findViewById(R.id.recyclerview).getId());
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adView.setLayoutParams(layoutParams);
adView.setAdUnitId(BuildConfig.FLAVOR.equals("dev") ? AppConstants.TEST_BANNER_ID : AppConstants.FRAGMENT_LIST_BANNER_ID);
adView.setAdSize(AdSize.BANNER);
relativeLayout.addView(adView);
adView.setId(Util.getIdNotUsed(requireActivity()));
adView.loadAd(new AdRequest.Builder().build());
RecyclerView rv = view.findViewById(R.id.recyclerview);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams2.addRule(RelativeLayout.ABOVE, adView.getId());
rv.setLayoutParams(layoutParams2);
}
So how can I make the last item of recycler view to be above ad? If there is anything you need more just tell me. Thank you.
CodePudding user response:
You can add the adview and set its visibility to GONE when the ad is not displaying & make it visible while displaying the ad and constrain the recyclerview to its top.
CodePudding user response:
I found a problem. I should have put SwipeRefreshLayout instead of RecyclerView rv = view.findViewById(R.id.recyclerview); to add ABOVE.