I'm trying to implement UnifiedNativeAd in a dynamic layout and here is my code
MobileAds.initialize(requireActivity(), new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
p.setMargins(0, 40, 0, 20);
LinearLayout layoutad = root.findViewById(R.id.layoutiklan);
AdView adView = new AdView(requireActivity());
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(getResources().getString(R.string.bannerads));
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
layoutad.addView(adView, p);
final FrameLayout framenative = new FrameLayout(requireContext());
layoutad.addView(framenative, p);
final UnifiedNativeAd[] nativeAd = new UnifiedNativeAd[1];
AdLoader.Builder builder = new AdLoader.Builder(requireContext(), getResources().getString(R.string.nativeads));
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
@Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
if (nativeAd[0] != null) {
nativeAd[0].destroy();
}
nativeAd[0] = unifiedNativeAd;
if (activity != null && isAdded()) {
UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.nativeads, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
framenative.addView(adView);
}
}
});
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(true)
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
}
}).build();
adLoader.loadAds(new AdRequest.Builder().build(), 50);
I just want to ask about :
- Is it possible to load more than one Native Ad ? Because I've tried this line
adLoader.loadAds(new AdRequest.Builder().build(), 50);
but it loads only one ad.
- If #1 is possible, do I need to implement a
for loops
to load more Native Ad ?
question from:
https://stackoverflow.com/questions/65915788/bulk-loading-unified-native-ad-from-google 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…