How do I place that ad that occupies the entire screen in my application, and is only closed when the person clicks the X?
How do I place that ad that occupies the entire screen in my application, and is only closed when the person clicks the X?
Full-screen ads, called Interstitial Ads covers the entire application screen, such as < in> FullScreen . See below as directed and described in the own documentation :
First you must instantiate an InterstitialAd object
...
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
requestNewInterstitial();
...
In the button action you should check if your ad was loaded using isLoad (). See:
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
See the complete code for what your main should look like:
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNewGameButton = (Button) findViewById(R.id.newgame_button);
mInterstitialAd = new InterstitialAd(this);
// aqui o seu adUnitId
mInterstitialAd.setAdUnitId("seu adUnitID aqui");
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
mNewGameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
}
});
beginPlayingGame();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
private void beginPlayingGame() {
// Play for a while, then display the New Game Button
}
}