How to place AdMob Native Advanced Ads in RecyclerView Android using Kotlin?

288 Views Asked by At

There are many questions asked here but I couldn't find appropriate answer.

I would like to show native ads in my RecyclerView after every 5 items is displayed. If you got full steps I will appreciate it.

Anyone with full step of the code integration is very helpfull.

Thanks.

1

There are 1 best solutions below

0
On

I understand you want to show native ads in position after every 5 items. First you need the code to create the ad, if you can't display it yet, please refer to enter link description here

Next create layout for native ads similar to creating items of Recyclerview

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:background="#282750"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/s_1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

            <com.google.android.gms.ads.nativead.MediaView
                android:id="@+id/mv_ads"
                android:layout_width="0dp"
                android:layout_height="180dp"
                app:cardCornerRadius="@dimen/s_5"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

</com.google.android.gms.ads.nativead.NativeAdView>

in recyclerview you want to display please add code

 @NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    context = parent.getContext();
    if (viewType == VIEW_TYPE_ITEM) {
        return new Main2ViewHolder(
                ItemMain2Binding.inflate(
                        LayoutInflater.from(parent.getContext()),
                        parent,
                        false
                )
        );
    } else  {
        return new NativeAdsViewHolder(
                ItemNativeAdsBinding.inflate(
                        LayoutInflater.from(parent.getContext()),
                        parent,
                        false
                )
        );
    }
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    if (holder.getItemViewType() == VIEW_TYPE_ITEM) {
        Main2ViewHolder itemHolder = (Main2ViewHolder) holder;
        itemHolder.setData(list.get(position));
        itemHolder.binding.getRoot().setOnClickListener(v -> {
            listener.click(list.get(position));
        });
    }
    if (holder.getItemViewType() == VIEW_TYPE_NATIVE_ADS) {
        NativeAdsViewHolder adsViewHolder = (NativeAdsViewHolder) holder;
        adsViewHolder.setData();
    }
}


@Override
public int getItemViewType(int position) {
    int viewType;
    if (position == 5) {
        viewType = VIEW_TYPE_NATIVE_ADS;
    }else {
        viewType = VIEW_TYPE_ITEM;
    }
    return viewType;
}

Finally, make sure in your list, for every 5th position, you add any value, so that it replaces the position of the ad.