Search code examples
androidinterstitial

Admob Interstitial show before game start


I want to add an interstitial ad on my game just before the game starts.

When you run the app, you have 3 diferent game modes so when you select one, the game starts on that mode. The idea is to show the interstitial after you press the button, just before the game starts.

I have followed the guide on the android developers page, here, but this doesn't fit exactly what I need. I have modified a bit to fit with my code, but the interstitial is not shown before starting the game, it is shown when you finish the game and returning to mainActivity.

This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn1Player = (ImageButton) findViewById(R.id.OnePlayerImgBtn);
    btnVersus = (ImageButton) findViewById(R.id.VersusImgBtn);
    btnLocalMultiP = (ImageButton) findViewById(R.id.LCLMultiPlayerImgBtn);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getString(R.string.ad_intersticial_1_id));
    requestNewInterstitial();


    btn1Player.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayInterstitial();
            startGame(false,true,MODE_SINGLE);
        }
    });
    btnVersus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayInterstitial();
            startGame(false,true,MODE_VERSUS);
        }
    });
    btnLocalMultiP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayInterstitial();
            startGame(true,true,MODE_LOCALMULTI);
        }
    });

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
        }
    });
}



private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
}


public void displayInterstitial() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    }
}

Solution

  • String singlemode ="Off";
    String versus ="Off";
    String multi ="Off";
    
    btn1Player.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayInterstitial();
            String singlemode ="On";
        }
    });
    btnVersus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayInterstitial();
             String versus ="On";
        }
    });
    btnLocalMultiP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            displayInterstitial();
            String multi="On";
        }
    });
    
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
    
    if singlemode.equals("On");
    {startGame(false,true,MODE_SINGLE);}
    
    if versus.equals("On");
    {startGame(false,true,MODE_VERSUS);}
    
    if multimode.equals("On");
    {startGame(true,true,MODE_LOCALMULTI);}
            requestNewInterstitial();
        }
    });