Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

android - bulk loading unified native ad from google

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 :

  1. 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.
  2. 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...