Search code examples
javaandroidadmobinterstitial

Admob Interstitial show on any Button Click


how add Admob Interstitial show on any Button Click Now it work automatically

mainactiviti

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;

public class PicSelect extends SherlockActivity {

private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
private Itemadapter imageAdapter;
private AdView mAdView;
private InterstitialAd mInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picselct);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#c5d951")));
    mAdView = (AdView) findViewById(R.id.adViewad);
    mAdView.loadAd(new AdRequest.Builder().build());

    mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId(getResources().getString(R.string.admob_intertesti
     al_id));
     mInterstitial.loadAd(new AdRequest.Builder().build());


     mInterstitial.setAdListener(new AdListener() {
          @Override
        public void onAdLoaded() {
            // TODO Auto-generated method stub
            super.onAdLoaded();
            if (mInterstitial.isLoaded()) {
                mInterstitial.show();
          }
        }
    });

    mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
    mPhotoSpacing = 
    getResources().getDimensionPixelSize(R.dimen.photo_spacing);
    photoGrid = (GridView) findViewById(R.id.albumGrid);

I tried a lot of solutions did not work for me. plz help me thnx


Solution

  • Please remove mInterstitial.show(); from onAdLoaded() and put it in inside of your button click calling method like below

    btnShow.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (mInterstitial.isLoaded()) {
                                        mInterstitial.show();
                                }
                            });