1

I'm having a problem with an Interstitial Ad that is not displaying. This is the activity that I want to show the ad.

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.enjoyyourriding.example.diana.mountainbikequest.DbHelper.DbHelper;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

import static com.enjoyyourriding.example.diana.mountainbikequest.R.id.txtTotalScore;

public class Done extends AppCompatActivity {
    Button btnTryAgain;
    TextView txtResultScore, txtResultQuestions;
    ProgressBar progressBarResult;
private InterstitialAd interstitialAd;
    final String TAG = this.getClass().getName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_done);
        AdView adview = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
        adview.loadAd(adRequest);

        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId("ca-app-pub-6594925022395892/2492722367");
        interstitialAd.loadAd(adRequest);
        Typeface mytupeface = Typeface.createFromAsset(getAssets(), "Helveticrap.ttf");
        TextView myTextView = (TextView) findViewById(R.id.textView);
        myTextView.setTypeface(mytupeface);
        DbHelper db = new DbHelper(this);
        btnTryAgain = (Button) findViewById(R.id.btnTryAgain);
        txtResultScore = (TextView) findViewById(txtTotalScore);
        txtResultQuestions = (TextView) findViewById(R.id.txttotalQuestions);
        btnTryAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);

                finish();
            }

        });

        adview.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }



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

            @Override
            public void onAdLeftApplication() {
                finish();
            }

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

        Bundle extra = getIntent().getExtras();
        if (extra != null) {
            int score = extra.getInt("SCORE");
            int totalQuestions = extra.getInt("TOTAL");
            int correctAnswer = extra.getInt("CORRECT");
            txtResultScore.setText(String.format("SCORE : %d", score));
            txtResultQuestions.setText(String.format("PASSED : %d/%d", correctAnswer, totalQuestions));

            if (score == 225) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage5,
                        (ViewGroup) findViewById(R.id.custommessage5));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("You are one of the best!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);
                    toast.show();
                    db.insertScore(score);
                }
            }else if (score == 250) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage6,
                        (ViewGroup) findViewById(R.id.custommessage6));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("You are true mountain bike legend!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);


                }

            }else if (score == 200) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage4,
                        (ViewGroup) findViewById(R.id.custommessage4));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("Well done, you passed 200 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }else if (score == 170) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage7,
                        (ViewGroup) findViewById(R.id.custommessage7));

                TextView text = (TextView) layout.findViewById(R.id.textView8);
                text.setText("Well done, you passed 170 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }
            }else if (score == 140) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage3,
                        (ViewGroup) findViewById(R.id.custommessage3));

                TextView text = (TextView) layout.findViewById(R.id.textView7);
                text.setText("Congratulations, you passed 140 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }else if (score == 110) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage8,
                        (ViewGroup) findViewById(R.id.custommessage8));

                TextView text = (TextView) layout.findViewById(R.id.textView7);
                text.setText("Congratulations, you passed 110 questions!");


                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);

                toast.show();
                db.insertScore(score);


            }else if (score == 30) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.customtoast,
                        (ViewGroup) findViewById(R.id.customtoast));

                TextView text = (TextView) layout.findViewById(R.id.textView5);
                text.setText("Congratulations, you passed 30 questions!");
                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);

                }
            }else if (score == 50) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage9,
                        (ViewGroup) findViewById(R.id.custommessage9));

                TextView text = (TextView) layout.findViewById(R.id.textView7);
                text.setText("Congratulations, you passed 50 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }else if (score == 80) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custommessage2,
                        (ViewGroup) findViewById(R.id.custommessage2));

                TextView text = (TextView) layout.findViewById(R.id.textView6);
                text.setText("Congratulations, you passed 80 questions!");

                for (int i = 0; i < 2; i++) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(layout);

                    toast.show();
                    db.insertScore(score);
                }

            }
            else if(score>=51&& score <=70  ){
               if (interstitialAd.isLoaded()){
                   interstitialAd.show();
               }
                interstitialAd.loadAd(adRequest);

            }


            db.insertScore(score);
        }
    }

    boolean twice;

    @Override
    public void onBackPressed() {

        Log.d(TAG, "click");

        if(twice==true){
            Intent i = new Intent(Intent.ACTION_MAIN);
            i.addCategory(Intent.CATEGORY_HOME);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            finish();


        }
        twice = true;
        Log.d(TAG, "twice:" + twice);

        Toast.makeText(Done.this,"Please press BACK again to exit the application",Toast.LENGTH_LONG).show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                twice = false;
                Log.d(TAG, "twice:" + twice);
            }
        }, 3000);

    }

    }

I've read everything about the Interstitial Ad in Admob. But, nothing works. Could the banner be the problem?

rupinderjeet
  • 2,984
  • 30
  • 54
Boqn
  • 21
  • 4

1 Answers1

0

Change interstitialAd = new InterstitialAd(this); to
interstitialAd = new InterstitialAd(getContext());

And also check if the adUnitId is correct.

Otherwise everything else seems fine. And, no, Interstitial ad wont be affected by the banner in any form since both are completely different entities and are independent of each other.

Abhriya Roy
  • 1,338
  • 17
  • 23