user3704212 user3704212 - 24 days ago 12
Android Question

How To display Admob interstitial every time a button is clicked

I would like to know how to display a Admob interstitial ad every time a button is pressed. I have managed to display a interstitial ad, but only on the first time the button is pressed. Here is my code:

package com.mycash.borgjake.mycash;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.TextView;

import android.view.View;

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.mycash.borgjake.mycash.R.styleable.View;

public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstitial;


Button btnClick;
Button btnWithdraw;

TextView txtBalance;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AdView adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
adView.loadAd(adRequest);

btnClick = (Button) findViewById(R.id.button);

txtBalance = (TextView) findViewById(R.id.textView);

btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String countValue = txtBalance.getText().toString();
int dblCountValue = Integer.parseInt(countValue);

if(mInterstitial.isLoaded()) {
mInterstitial.show();
}

dblCountValue++;
txtBalance.setText(String.valueOf(dblCountValue));


}
});

mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId("...");
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mInterstitial.loadAd(request);

}

public void onButtonClick(View v) {
if(v.getId() == R.id.button2) {
Intent i = new Intent(MainActivity.this, Withdraw.class);
startActivity(i);
}
}
}


Thanks for the help in advance :)

Answer

You need to call

mInterstitial.loadAd(request);

in your button click listener after showing it.

Final code ;

package com.mycash.borgjake.mycash;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.TextView;

import android.view.View;

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.mycash.borgjake.mycash.R.styleable.View;

public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstitial;


Button btnClick;
Button btnWithdraw;

TextView txtBalance;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AdView adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();
adView.loadAd(adRequest);

btnClick = (Button) findViewById(R.id.button);

txtBalance = (TextView) findViewById(R.id.textView);

btnClick.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String countValue = txtBalance.getText().toString();
        int dblCountValue = Integer.parseInt(countValue);

        if(mInterstitial.isLoaded()) {
            mInterstitial.show();
        }

        mInterstitial.loadAd(request);

        dblCountValue++;
        txtBalance.setText(String.valueOf(dblCountValue));


    }
});

mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId("...");
AdRequest request = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();
mInterstitial.loadAd(request);

}

public void onButtonClick(View v) {
if(v.getId() == R.id.button2) {
    Intent i = new Intent(MainActivity.this, Withdraw.class);
    startActivity(i);
    }
  }
}

Just copy&paste edited code. :) then read the following explainations.

Comments