use of com.google.android.gms.ads.AdLoader 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());
}
});
}
Aggregations