1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Drain Droids

Android Studio For Beginners.Mobile Apps.Android Games.

Sunday, January 6, 2019

Android Studio:How to Display Interstitial Ads after 3 Button Click


In the previous post, we discussed about how to show Banner ads, but in this short article we are going to make app to display interstitial ads when a button is clicked.
Note, this is not to display at each button click, I will make another guide post for that but at this time, we are demonstrating after when third activity is performed. This means, users perform the first click, then second click and third click before your ads shows up.
You can also choose to make it shows up after two button click which means it will appear after second activity is performed on your app.
Admob like this type of ads display. It won’t get you into trouble because it does not go against their ads policy.
Step 1
Go to project-level build.gradle Project paste the bellow code on jcenter of allprojects
maven {
            url "https://maven.google.com"
        }
Note: if you already have done this before, you don’t have to implement this again.
Step 2
Go to app-level build.gradle app paste the below code
implementation 'com.google.android.gms:play-services-ads:15.0.0'

Again, if you already have done this before, you don’t have to implement this again.

Step 3
On the main Activity Java, import the bellow Code
import com.google.android.gms.ads.MobileAds;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.InterstitialAd;

Step 4
Paste the code below under AppCompatActivity
int count = 0;
    private InterstitialAd mInterstitialAd;

Step 5
Copy and paste the below code on the onCreate under setContentView(R.layout.activity_main);
 
MobileAds.initialize(this, "ca-app-pub-1616194098354243~2617089868");
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }
           
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when the ad is displayed.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
        });
    }
private void showAds() {
        ++count;
        if (count % 3 == 0 && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }

Now, you can change the 3 to 2 if you want it to display after  two button click.

Step 6
Paste this code on all your intent  showAds();
You will have something like this
}
    public void ten(View view) {
        Intent intent = new Intent(MainActivity.this, salvationone.class);
        startActivity(intent);
        showAds();

Done. Good luck with your android development.