Search in sources :

Example 1 with NativeAdView

use of com.google.android.gms.ads.nativead.NativeAdView in project mopub-android-mediation by mopub.

the class GooglePlayServicesAdRenderer method insertGoogleNativeAdView.

/**
 * This method will add the given Google native ad view into the view hierarchy of the given
 * MoPub native ad view.
 *
 * @param googleNativeAdView Google's native ad view to be added as a parent to the MoPub's
 *                           view.
 * @param moPubNativeAdView  MoPub's native ad view created by this renderer.
 * @param swapMargins        {@code true} if the margins need to be swapped, {@code false}
 *                           otherwise.
 */
private static void insertGoogleNativeAdView(NativeAdView googleNativeAdView, View moPubNativeAdView, boolean swapMargins) {
    MoPubLog.log(SHOW_ATTEMPTED, ADAPTER_NAME);
    if (moPubNativeAdView instanceof FrameLayout && moPubNativeAdView.getId() == ID_WRAPPING_FRAME) {
        googleNativeAdView.setId(ID_GOOGLE_NATIVE_VIEW);
        FrameLayout outerFrame = (FrameLayout) moPubNativeAdView;
        View actualView = outerFrame.getChildAt(0);
        if (swapMargins) {
            // Google native ad view renders the AdChoices icon in one of the four corners of
            // its view. If a margin is specified on the actual ad view, the AdChoices view
            // might be rendered outside the actual ad view. Moving the margins from the
            // actual ad view to Google native ad view will make sure that the AdChoices icon
            // is being rendered within the bounds of the actual ad view.
            FrameLayout.LayoutParams googleNativeAdViewParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            FrameLayout.LayoutParams actualViewParams = (FrameLayout.LayoutParams) actualView.getLayoutParams();
            googleNativeAdViewParams.setMargins(actualViewParams.leftMargin, actualViewParams.topMargin, actualViewParams.rightMargin, actualViewParams.bottomMargin);
            googleNativeAdView.setLayoutParams(googleNativeAdViewParams);
            actualViewParams.setMargins(0, 0, 0, 0);
        } else {
            googleNativeAdView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        }
        outerFrame.removeView(actualView);
        googleNativeAdView.addView(actualView);
        outerFrame.addView(googleNativeAdView);
    } else {
        MoPubLog.log(CUSTOM, ADAPTER_NAME, "Couldn't add Google native ad view. Wrapping view not found.");
    }
}
Also used : ViewGroup(android.view.ViewGroup) FrameLayout(android.widget.FrameLayout) NativeAdView(com.google.android.gms.ads.nativead.NativeAdView) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) MediaView(com.google.android.gms.ads.nativead.MediaView) AdChoicesView(com.google.android.gms.ads.nativead.AdChoicesView)

Example 2 with NativeAdView

use of com.google.android.gms.ads.nativead.NativeAdView in project mopub-android-mediation by mopub.

the class GooglePlayServicesAdRenderer method renderAdView.

@Override
public void renderAdView(@NonNull View view, @NonNull GooglePlayServicesNativeAd nativeAd) {
    Preconditions.checkNotNull(view);
    Preconditions.checkNotNull(nativeAd);
    GoogleStaticNativeViewHolder viewHolder = mViewHolderMap.get(view);
    if (viewHolder == null) {
        viewHolder = GoogleStaticNativeViewHolder.fromViewBinder(view, mViewBinder);
        mViewHolderMap.put(view, viewHolder);
    }
    NativeAdView nativeAdView = new NativeAdView(view.getContext());
    updateNativeAdview(nativeAd, viewHolder, nativeAdView);
    insertGoogleNativeAdView(nativeAdView, view, nativeAd.shouldSwapMargins());
}
Also used : NativeAdView(com.google.android.gms.ads.nativead.NativeAdView)

Example 3 with NativeAdView

use of com.google.android.gms.ads.nativead.NativeAdView in project googleads-mobile-android-mediation by googleads.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    adapterRadioButton = findViewById(R.id.integration_adapter);
    // Banner ads.
    Button loadBannerButton = findViewById(R.id.banner_load_ad);
    loadBannerButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            adView = new AdView(view.getContext());
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(getBannerAdUnitId());
            adView.setAdListener(new AdListener() {

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    Toast.makeText(MainActivity.this, "Failed to load banner: " + loadAdError, Toast.LENGTH_SHORT).show();
                }
            });
            adView.loadAd(new AdRequest.Builder().build());
            // Add banner to view hierarchy.
            FrameLayout bannerContainer = findViewById(R.id.banner_container);
            bannerContainer.removeAllViews();
            bannerContainer.addView(adView);
        }
    });
    // Interstitial ads.
    loadInterstitialButton = (Button) findViewById(R.id.interstitial_load_button);
    loadInterstitialButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            loadInterstitialButton.setEnabled(false);
            InterstitialAd.load(MainActivity.this, getInterstitialAdUnitId(), new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {

                @Override
                public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                    interstitial = interstitialAd;
                    interstitial.setFullScreenContentCallback(new FullScreenContentCallback() {

                        @Override
                        public void onAdFailedToShowFullScreenContent(@NonNull AdError error) {
                            Toast.makeText(MainActivity.this, "Failed to show interstitial: " + error, Toast.LENGTH_SHORT).show();
                            loadInterstitialButton.setEnabled(true);
                        }

                        @Override
                        public void onAdDismissedFullScreenContent() {
                            loadInterstitialButton.setEnabled(true);
                        }
                    });
                    showInterstitialButton.setEnabled(true);
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    Toast.makeText(MainActivity.this, "Failed to load interstitial: " + loadAdError, Toast.LENGTH_SHORT).show();
                    interstitial = null;
                    loadInterstitialButton.setEnabled(true);
                }
            });
        }
    });
    showInterstitialButton = (Button) findViewById(R.id.interstitial_show_button);
    showInterstitialButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            showInterstitialButton.setEnabled(false);
            if (interstitial != null) {
                interstitial.show(MainActivity.this);
            }
        }
    });
    // Sample Adapter Rewarded Ad Button.
    loadRewardedButton = (Button) findViewById(R.id.rewarded_load_button);
    loadRewardedButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            loadRewardedButton.setEnabled(false);
            RewardedAd.load(MainActivity.this, getRewardedAdUnitId(), new AdRequest.Builder().build(), new RewardedAdLoadCallback() {

                @Override
                public void onAdLoaded(@NonNull RewardedAd ad) {
                    rewardedAd = ad;
                    rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {

                        @Override
                        public void onAdFailedToShowFullScreenContent(@NonNull AdError error) {
                            Toast.makeText(MainActivity.this, "Failed to show interstitial: " + error, Toast.LENGTH_SHORT).show();
                            loadRewardedButton.setEnabled(true);
                        }

                        @Override
                        public void onAdDismissedFullScreenContent() {
                            loadRewardedButton.setEnabled(true);
                        }
                    });
                    showRewardedButton.setEnabled(true);
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    Toast.makeText(MainActivity.this, "Failed to load rewarded ad: " + loadAdError, Toast.LENGTH_SHORT).show();
                    rewardedAd = null;
                    loadRewardedButton.setEnabled(true);
                }
            });
        }
    });
    showRewardedButton = (Button) findViewById(R.id.rewarded_show_button);
    showRewardedButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            showRewardedButton.setEnabled(false);
            if (rewardedAd != null) {
                rewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() {

                    @Override
                    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                        Toast.makeText(MainActivity.this, String.format("User earned reward. Type: %s, amount: %d", rewardItem.getType(), rewardItem.getAmount()), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }
    });
    // Native ads.
    final Button nativeLoadButton = (Button) findViewById(R.id.native_load_ad);
    nativeLoadButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            adLoader = new AdLoader.Builder(view.getContext(), getNativeAdUnitId()).forNativeAd(new NativeAd.OnNativeAdLoadedListener() {

                @Override
                public void onNativeAdLoaded(@NonNull NativeAd nativeAd) {
                    FrameLayout nativeContainer = findViewById(R.id.native_container);
                    NativeAdView adView = (NativeAdView) getLayoutInflater().inflate(R.layout.native_ad, null);
                    populateNativeAdView(nativeAd, adView);
                    nativeContainer.removeAllViews();
                    nativeContainer.addView(adView);
                }
            }).withAdListener(new AdListener() {

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError error) {
                    Toast.makeText(MainActivity.this, "Failed to load native ad: " + error, Toast.LENGTH_SHORT).show();
                }
            }).build();
            adLoader.loadAd(new AdRequest.Builder().build());
        }
    });
}
Also used : LoadAdError(com.google.android.gms.ads.LoadAdError) OnUserEarnedRewardListener(com.google.android.gms.ads.OnUserEarnedRewardListener) RewardedAdLoadCallback(com.google.android.gms.ads.rewarded.RewardedAdLoadCallback) AdRequest(com.google.android.gms.ads.AdRequest) RadioButton(android.widget.RadioButton) Button(android.widget.Button) RewardedAd(com.google.android.gms.ads.rewarded.RewardedAd) NonNull(androidx.annotation.NonNull) InterstitialAdLoadCallback(com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback) RewardItem(com.google.android.gms.ads.rewarded.RewardItem) InterstitialAd(com.google.android.gms.ads.interstitial.InterstitialAd) NativeAdView(com.google.android.gms.ads.nativead.NativeAdView) AdView(com.google.android.gms.ads.AdView) LoadAdError(com.google.android.gms.ads.LoadAdError) AdError(com.google.android.gms.ads.AdError) AdLoader(com.google.android.gms.ads.AdLoader) OnClickListener(android.view.View.OnClickListener) NativeAdView(com.google.android.gms.ads.nativead.NativeAdView) ImageView(android.widget.ImageView) View(android.view.View) NativeAdView(com.google.android.gms.ads.nativead.NativeAdView) AdView(com.google.android.gms.ads.AdView) TextView(android.widget.TextView) MediaView(com.google.android.gms.ads.nativead.MediaView) AdListener(com.google.android.gms.ads.AdListener) NativeAd(com.google.android.gms.ads.nativead.NativeAd) FrameLayout(android.widget.FrameLayout) FullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback) OnClickListener(android.view.View.OnClickListener)

Aggregations

NativeAdView (com.google.android.gms.ads.nativead.NativeAdView)3 View (android.view.View)2 FrameLayout (android.widget.FrameLayout)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 MediaView (com.google.android.gms.ads.nativead.MediaView)2 OnClickListener (android.view.View.OnClickListener)1 ViewGroup (android.view.ViewGroup)1 Button (android.widget.Button)1 RadioButton (android.widget.RadioButton)1 NonNull (androidx.annotation.NonNull)1 AdError (com.google.android.gms.ads.AdError)1 AdListener (com.google.android.gms.ads.AdListener)1 AdLoader (com.google.android.gms.ads.AdLoader)1 AdRequest (com.google.android.gms.ads.AdRequest)1 AdView (com.google.android.gms.ads.AdView)1 FullScreenContentCallback (com.google.android.gms.ads.FullScreenContentCallback)1 LoadAdError (com.google.android.gms.ads.LoadAdError)1 OnUserEarnedRewardListener (com.google.android.gms.ads.OnUserEarnedRewardListener)1 InterstitialAd (com.google.android.gms.ads.interstitial.InterstitialAd)1