Google Ads in android

 How to used Banner Ads 

//loading Google Adds on our app
private AdView adView;
private AdRequest adRequest;
private void BannerLoadAds(){
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});

adView = findViewById(R.id.adView);
adView.loadAd(adRequest);

adView.setAdListener(new AdListener() {
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}

@Override
public void onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}

@Override
public void onAdFailedToLoad(LoadAdError adError) {
// Code to be executed when an ad request fails.
adView.loadAd(adRequest);
Toast.makeText(MainActivity.this, adError.getMessage(), Toast.LENGTH_SHORT).show();
}

@Override
public void onAdImpression() {
// Code to be executed when an impression is recorded
// for an ad.
}

@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
super.onAdLoaded();
Toast.makeText(MainActivity.this, "Loading ads", Toast.LENGTH_SHORT).show();
}

@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
});

}
AdRequest adRequest = new AdRequest.Builder().build();

How to used Interstitial Ads

//    load InterstitialAd
private AdView adView;
private AdRequest adRequest;
private InterstitialAd minterstitialAd;
private void InterstitialAdsLoad(){

InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
Log.d("Error", "failed to load error");
}

@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
super.onAdLoaded(interstitialAd);
minterstitialAd = interstitialAd;
minterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
super.onAdClicked();
}

@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
}

@Override
public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
super.onAdFailedToShowFullScreenContent(adError);
}

@Override
public void onAdImpression() {
super.onAdImpression();
}

@Override
public void onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent();
minterstitialAd = null;
}
});
}
});

final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
if (minterstitialAd != null) {
minterstitialAd.show(MainActivity.this);
} else {
Toast.makeText(MainActivity.this, "Load", Toast.LENGTH_SHORT).show();
}
handler.postDelayed(this,1000);
}
};

handler.post(runnable);

}

How to used Rewarded insterstitial

  btns.setOnClickListener(v->{
if(rewardedInterstitialAd != null){
rewardedInterstitialAd.show(/* Activity */ ActivityLoadAds.this,/*
OnUserEarnedRewardListener */ ActivityLoadAds.this);
}else{
Toast.makeText(this, "adds not Loaded", Toast.LENGTH_SHORT).show();
}
});
}

// after click button then show video the user watch video and can give a coin
private void RewardedAdshow(){
// Use the test ad unit ID to load an ad.
RewardedInterstitialAd.load(ActivityLoadAds.this, "ca-app-pub-3940256099942544/5354046379",
new AdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
Log.d(TAG, "Ad was loaded.");
rewardedInterstitialAd = ad;
rewardedInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}

@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
rewardedInterstitialAd = null;
RewardedAdshow();
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedInterstitialAd = null;
}

@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}

@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});

}

@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Toast.makeText(this, "User earned reward.", Toast.LENGTH_SHORT).show();
int i = Integer.parseInt(offertext.getText().toString());
offertext.setText(String.valueOf(i+1));
offerimage.setImageResource(R.drawable.ic_launcher_foreground);
}





















Comments

Popular posts from this blog

Media Player in android

Fetch data from Api in android using Volley library

GridView in android