0

I am developing an app of wallpapers. In it images change with buttons. I want to show interstitial when user almost reaches at 10th wallpaper and so on. Now I am using this method for interstitial and it works perfectly but through this interstitial shows immediately as app starts. I also want interstitial as user ends the app. Here is code.

 // Create the interstitial 
     interstitial = new InterstitialAd(this, MY_INTERSTITIAL_UNIT_ID);
  // Create ad request
    AdRequest adRequest = new AdRequest();
  // Begin loading your interstitial
    interstitial.loadAd(adRequest);
  // Set Ad Listener to use the callbacks below
    interstitial.setAdListener(this);
} 
  @Override public void onReceiveAd(Ad ad) { 
   Log.d("OK", "Received ad");
  if (ad == interstitial) { 
   interstitial.show();
 } 
} 
 }

Main

 int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 }; 
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 
   hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
    iButton = (Button) findViewById(R.id.bNext);
    gButton = (Button) findViewById(R.id.bPrev);
  //Just set one Click listener for the image 
    iButton.setOnClickListener(iButtonChangeImageListener); 
    gButton.setOnClickListener(gButtonChangeImageListener); 
  }
     View.OnClickListener iButtonChangeImageListener = new OnClickListener() { 
      public void onClick(View v) { 
   //Increase Counter to move to next Image 
     currentImage++;
     currentImage = currentImage % images.length; 
       hImageViewPic.setImageResource(images[currentImage]);
    } 
  };
     View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
     public void onClick(View v) {
   //Increase Counter to move to next Image 
     currentImage--;
     currentImage = (currentImage + images.length) % images.length; 
       hImageViewPic.setImageResource(images[currentImage]); 
      }
    };

Plz guide me how can I solve this issue. Thanx

HBBR
  • 77
  • 1
  • 7

1 Answers1

0

Call this method:-

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

}

inside onClick() method:-

 View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
 public void onClick(View v) {

 currentImage--;
 currentImage = (currentImage + images.length) % images.length; 
   hImageViewPic.setImageResource(images[currentImage]); 

  }
};

Have an if statement inside the above onClick code to check if the image is 10. Just include

displayInterstitial();

in that if. And it will only show interstitial after the 10th image.

Kunalxigxag
  • 728
  • 4
  • 12