com.google.android.gms.ads.AdLoader Java Examples

The following examples show how to use com.google.android.gms.ads.AdLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdMobNativeAd.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
/**
 * AppNexus SDk calls this method to request a native ad from AdMob
 *
 * @param context The context from which this class is instantiated.
 * @param uid     AdMob ad unit id, app developer needs to set up account with AdMob.
 * @param mBC     The controller that passes callbacks to AppNexus SDK
 * @param tp      Targeting parameters that were set in AppNexus API.
 */
@Override
public void requestNativeAd(Context context, String parameterString, String uid, MediatedNativeAdController mBC, TargetingParameters tp) {
    if (mBC != null) {

        AdMobNativeListener adMobNativeListener = new AdMobNativeListener(mBC);

        NativeAdOptions.Builder adOptionsBuilder = new NativeAdOptions.Builder();

        if(!AdMobNativeSettings.enableMediaView){
            adOptionsBuilder.setReturnUrlsForImageAssets(true);
        }

        if(AdMobNativeSettings.videoOptions!=null) {
            adOptionsBuilder.setVideoOptions(AdMobNativeSettings.videoOptions);
        }


        AdLoader.Builder builder = new AdLoader.Builder(context, uid);
        builder.withNativeAdOptions(adOptionsBuilder.build());
        builder.forUnifiedNativeAd(adMobNativeListener);
        builder.build().loadAd(GooglePlayServicesBanner.buildRequest(tp));
    }


}
 
Example #2
Source File: StoriesFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #3
Source File: StoriesOverview.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #4
Source File: DownloadHistoryPhotoFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #5
Source File: DownloadHistoryVideoFragment.java    From Instagram-Profile-Downloader with MIT License 5 votes vote down vote up
private void loadNativeAds() {

        AdLoader.Builder builder = new AdLoader.Builder(context, getString(R.string.native_ad_id));
        adLoader = builder.forUnifiedNativeAd(
                new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                    @Override
                    public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                        // A native ad loaded successfully, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        mNativeAds.add(unifiedNativeAd);
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).withAdListener(
                new AdListener() {
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        // A native ad failed to load, check if the ad loader has finished loading
                        // and if so, insert the ads into the list.
                        Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                                + " load another.");
                        if (!adLoader.isLoading()) {
                            insertAdsInMenuItems();
                        }
                    }
                }).build();

        // Load the Native Express ad.
    }
 
Example #6
Source File: MainActivity.java    From admob-native-advanced-feed with Apache License 2.0 5 votes vote down vote up
private void loadNativeAds() {

    AdLoader.Builder builder = new AdLoader.Builder(this, getString(R.string.ad_unit_id));
    adLoader = builder.forUnifiedNativeAd(
        new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
            @Override
            public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                // A native ad loaded successfully, check if the ad loader has finished loading
                // and if so, insert the ads into the list.
                mNativeAds.add(unifiedNativeAd);
                if (!adLoader.isLoading()) {
                    insertAdsInMenuItems();
                }
            }
        }).withAdListener(
        new AdListener() {
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // A native ad failed to load, check if the ad loader has finished loading
                // and if so, insert the ads into the list.
                Log.e("MainActivity", "The previous native ad failed to load. Attempting to"
                    + " load another.");
                if (!adLoader.isLoading()) {
                    insertAdsInMenuItems();
                }
            }
        }).build();

    // Load the Native ads.
    adLoader.loadAds(new AdRequest.Builder().build(), NUMBER_OF_ADS);
}
 
Example #7
Source File: AdmobFetcher.java    From admobadapter with Apache License 2.0 5 votes vote down vote up
/**
 * Subscribing to the native ads events
 */
protected synchronized void setupAds() {
    String unitId = getReleaseUnitId() != null ? getReleaseUnitId() : getDefaultUnitId();
    AdLoader.Builder adloaderBuilder = new AdLoader.Builder(mContext.get(), unitId)
            .withAdListener(new AdListener() {
                @Override
                public void onAdFailedToLoad(int errorCode) {
                    // Handle the failure by logging, altering the UI, etc.
                    Log.i(TAG, "onAdFailedToLoad " + errorCode);
                    lockFetch.set(false);
                    mFetchFailCount++;
                    mFetchingAdsCnt--;
                    ensurePrefetchAmount();
                    onAdFailed( mPrefetchedAdList.size(), errorCode, null);
                }
            })
            .withNativeAdOptions(new NativeAdOptions.Builder()
                    // Methods in the NativeAdOptions.Builder class can be
                    // used here to specify individual options settings.
                    .build());
    if(getAdTypeToFetch().contains(EAdType.ADVANCED_INSTALLAPP))
        adloaderBuilder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
                @Override
                public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
                    onAdFetched(appInstallAd);
                }
            });
    if(getAdTypeToFetch().contains(EAdType.ADVANCED_CONTENT))
        adloaderBuilder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
                @Override
                public void onContentAdLoaded(NativeContentAd contentAd) {
                    onAdFetched(contentAd);
                }
            });

    adLoader = adloaderBuilder.build();
}
 
Example #8
Source File: MainActivity.java    From android-ads with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new native ad based on the boolean parameters and calls the
 * corresponding "populate" method when one is successfully returned.
 *
 */
private void refreshAd() {
    refresh.setEnabled(false);

    AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            // You must call destroy on old ads when you are done with them,
            // otherwise you will have a memory leak.
            if (nativeAd != null) {
                nativeAd.destroy();
            }
            nativeAd = unifiedNativeAd;
            FrameLayout frameLayout =
                    findViewById(R.id.fl_adplaceholder);
            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                    .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        }

    });

    VideoOptions videoOptions = new VideoOptions.Builder()
            .setStartMuted(startVideoAdsMuted.isChecked())
            .build();

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
            .setVideoOptions(videoOptions)
            .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(MainActivity.this, "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());

    videoStatus.setText("");
}
 
Example #9
Source File: AdMobCustomMuteThisAdFragment.java    From android-ads with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new unified native ad based on the boolean parameters and calls the
 * "populateUnifiedNativeAdView" method when one is successfully returned.
 */
private void refreshAd() {
    refresh.setEnabled(false);
    muteButton.setEnabled(false);

    Resources resources = getActivity().getResources();
    AdLoader.Builder builder = new AdLoader.Builder(getActivity(),
            resources.getString(R.string.custommute_fragment_ad_unit_id));

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            AdMobCustomMuteThisAdFragment.this.nativeAd = unifiedNativeAd;
            muteButton.setEnabled(unifiedNativeAd.isCustomMuteThisAdEnabled());
            nativeAd.setMuteThisAdListener(new MuteThisAdListener() {
                @Override
                public void onAdMuted() {
                    muteAd();
                    Toast.makeText(getActivity(), "Ad muted", Toast.LENGTH_SHORT).show();
                }
            });

            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            adContainer.removeAllViews();
            adContainer.addView(adView);
        }

    });

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
        .setRequestCustomMuteThisAd(true)
        .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(getActivity(), "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());
}
 
Example #10
Source File: MainActivity.java    From googleads-mobile-android-examples with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new native ad based on the boolean parameters and calls the
 * corresponding "populate" method when one is successfully returned.
 *
 */
private void refreshAd() {
    refresh.setEnabled(false);

    AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            // If this callback occurs after the activity is destroyed, you must call
            // destroy and return or you may get a memory leak.
            if (isDestroyed()) {
                unifiedNativeAd.destroy();
                return;
            }
            // You must call destroy on old ads when you are done with them,
            // otherwise you will have a memory leak.
            if (nativeAd != null) {
                nativeAd.destroy();
            }
            nativeAd = unifiedNativeAd;
            FrameLayout frameLayout =
                    findViewById(R.id.fl_adplaceholder);
            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                    .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        }

    });

    VideoOptions videoOptions = new VideoOptions.Builder()
            .setStartMuted(startVideoAdsMuted.isChecked())
            .build();

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
            .setVideoOptions(videoOptions)
            .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(MainActivity.this, "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());

    videoStatus.setText("");
}
 
Example #11
Source File: AdMobCustomMuteThisAdFragment.java    From googleads-mobile-android-examples with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a request for a new unified native ad based on the boolean parameters and calls the
 * "populateUnifiedNativeAdView" method when one is successfully returned.
 */
private void refreshAd() {
    refresh.setEnabled(false);
    muteButton.setEnabled(false);

    Resources resources = getActivity().getResources();
    AdLoader.Builder builder = new AdLoader.Builder(getActivity(),
            resources.getString(R.string.custommute_fragment_ad_unit_id));

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            AdMobCustomMuteThisAdFragment.this.nativeAd = unifiedNativeAd;
            muteButton.setEnabled(unifiedNativeAd.isCustomMuteThisAdEnabled());
            nativeAd.setMuteThisAdListener(new MuteThisAdListener() {
                @Override
                public void onAdMuted() {
                    muteAd();
                    Toast.makeText(getActivity(), "Ad muted", Toast.LENGTH_SHORT).show();
                }
            });

            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            adContainer.removeAllViews();
            adContainer.addView(adView);
        }

    });

    NativeAdOptions adOptions = new NativeAdOptions.Builder()
        .setRequestCustomMuteThisAd(true)
        .build();

    builder.withNativeAdOptions(adOptions);

    AdLoader adLoader = builder.withAdListener(new AdListener() {
        @Override
        public void onAdFailedToLoad(int errorCode) {
            refresh.setEnabled(true);
            Toast.makeText(getActivity(), "Failed to load native ad: "
                    + errorCode, Toast.LENGTH_SHORT).show();
        }
    }).build();

    adLoader.loadAd(new AdRequest.Builder().build());
}