|
| 1 | +package com.d4rk.androidtutorials.java.ads.managers; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.view.LayoutInflater; |
| 5 | +import android.view.View; |
| 6 | +import android.view.ViewGroup; |
| 7 | +import android.widget.Button; |
| 8 | +import android.widget.ImageView; |
| 9 | +import android.widget.TextView; |
| 10 | + |
| 11 | +import androidx.annotation.NonNull; |
| 12 | + |
| 13 | +import com.d4rk.androidtutorials.java.R; |
| 14 | +import com.google.android.gms.ads.AdLoader; |
| 15 | +import com.google.android.gms.ads.AdRequest; |
| 16 | +import com.google.android.gms.ads.nativead.MediaView; |
| 17 | +import com.google.android.gms.ads.nativead.NativeAd; |
| 18 | +import com.google.android.gms.ads.nativead.NativeAdView; |
| 19 | + |
| 20 | +/** |
| 21 | + * Helper class to load AdMob native ads into a container. |
| 22 | + */ |
| 23 | +public class NativeAdLoader { |
| 24 | + |
| 25 | + public static void load(@NonNull Context context, @NonNull ViewGroup container) { |
| 26 | + AdLoader adLoader = new AdLoader.Builder(context, context.getString(R.string.ad_banner_unit_id)) |
| 27 | + .forNativeAd(nativeAd -> { |
| 28 | + LayoutInflater inflater = LayoutInflater.from(context); |
| 29 | + NativeAdView adView = (NativeAdView) inflater.inflate(R.layout.native_ad, container, false); |
| 30 | + populateNativeAdView(nativeAd, adView); |
| 31 | + container.removeAllViews(); |
| 32 | + container.addView(adView); |
| 33 | + }) |
| 34 | + .build(); |
| 35 | + adLoader.loadAd(new AdRequest.Builder().build()); |
| 36 | + } |
| 37 | + |
| 38 | + private static void populateNativeAdView(@NonNull NativeAd nativeAd, @NonNull NativeAdView adView) { |
| 39 | + adView.setMediaView((MediaView) adView.findViewById(R.id.ad_media)); |
| 40 | + adView.setHeadlineView(adView.findViewById(R.id.ad_headline)); |
| 41 | + adView.setBodyView(adView.findViewById(R.id.ad_body)); |
| 42 | + adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action)); |
| 43 | + adView.setIconView(adView.findViewById(R.id.ad_app_icon)); |
| 44 | + |
| 45 | + ((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline()); |
| 46 | + |
| 47 | + if (nativeAd.getBody() == null) { |
| 48 | + adView.getBodyView().setVisibility(View.GONE); |
| 49 | + } else { |
| 50 | + adView.getBodyView().setVisibility(View.VISIBLE); |
| 51 | + ((TextView) adView.getBodyView()).setText(nativeAd.getBody()); |
| 52 | + } |
| 53 | + |
| 54 | + if (nativeAd.getCallToAction() == null) { |
| 55 | + adView.getCallToActionView().setVisibility(View.GONE); |
| 56 | + } else { |
| 57 | + adView.getCallToActionView().setVisibility(View.VISIBLE); |
| 58 | + ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction()); |
| 59 | + } |
| 60 | + |
| 61 | + if (nativeAd.getIcon() == null) { |
| 62 | + adView.getIconView().setVisibility(View.GONE); |
| 63 | + } else { |
| 64 | + ((ImageView) adView.getIconView()).setImageDrawable(nativeAd.getIcon().getDrawable()); |
| 65 | + adView.getIconView().setVisibility(View.VISIBLE); |
| 66 | + } |
| 67 | + |
| 68 | + adView.setNativeAd(nativeAd); |
| 69 | + } |
| 70 | +} |
0 commit comments