Skip to content
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.d4rk.androidtutorials.java.ads.managers;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.d4rk.androidtutorials.java.R;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.nativead.MediaView;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdView;
Expand All @@ -23,12 +26,21 @@
*/
public class NativeAdLoader {

private static final String TAG = "NativeAdLoader";

public static void load(@NonNull Context context, @NonNull ViewGroup container) {
load(context, container, R.layout.large_home_banner_native_ad);
load(context, container, R.layout.large_home_banner_native_ad, null);
}

public static void load(@NonNull Context context, @NonNull ViewGroup container, @LayoutRes int layoutRes) {
AdLoader adLoader = new AdLoader.Builder(context, context.getString(R.string.native_ad_banner_unit_id))
load(context, container, layoutRes, null);
}

public static void load(@NonNull Context context,
@NonNull ViewGroup container,
@LayoutRes int layoutRes,
@androidx.annotation.Nullable AdListener listener) {
AdLoader.Builder builder = new AdLoader.Builder(context, context.getString(R.string.native_ad_banner_unit_id))
.forNativeAd(nativeAd -> {
LayoutInflater inflater = LayoutInflater.from(context);
NativeAdView adView = (NativeAdView) inflater.inflate(layoutRes, container, false);
Expand All @@ -41,8 +53,19 @@ public static void load(@NonNull Context context, @NonNull ViewGroup container,
populateNativeAdView(nativeAd, adView);
container.removeAllViews();
container.addView(adView);
})
.build();
container.requestLayout();
});

builder.withAdListener(listener != null ? listener : new AdListener() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.w(TAG, "Failed to load native ad: " + loadAdError.getMessage());
container.removeAllViews();
container.setVisibility(View.GONE);
}
});

AdLoader adLoader = builder.build();
adLoader.loadAd(new AdRequest.Builder().build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
import androidx.annotation.Nullable;

import com.d4rk.androidtutorials.java.R;
import com.google.android.gms.ads.AdRequest;
import com.d4rk.androidtutorials.java.ads.managers.NativeAdLoader;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;

/**
* Custom view that acts as a drop-in replacement for AdView and loads
Expand Down Expand Up @@ -45,7 +46,11 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs, int de
}

public void loadAd(AdRequest adRequest) {
NativeAdLoader.load(getContext(), this, layoutRes);
loadAd(adRequest, null);
}

public void loadAd(AdRequest adRequest, @Nullable AdListener listener) {
NativeAdLoader.load(getContext(), this, layoutRes, listener);
}

public void setNativeAdLayout(@LayoutRes int layoutRes) {
Expand Down
Loading