Skip to content

Integrating Full Screen Overlay Ad

rprunskas edited this page May 31, 2022 · 7 revisions

To load an interstitial ad, an AdOverlay class should be used. Before loading an interstitial ad, first it must be initialized.

Java:

		
private AdOverlay adOverlay;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_adoverlay);
    adOverlay = AdOverlay.createInstance(this);
}
    

Kotlin:

		
private lateinit var adOverlay: AdOverlay

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_adoverlay)
    adOverlay = AdOverlay.createInstance(this)
}
    

This class has two methods that can be used to display an interstitial:

  • loadAd() - used to load an ad, and store its contents into memory for later quick use. This is useful if an ad has lots of resources to load.
  • showAd() - used for showing an ad. If the ad is not loaded, it loads internally and displays it as soon as the ad is loaded.

You can find an example below how these functions are used with simple button events.

Java:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.load_button: {
            adLoader.loadAd();
            break;
        }
        case R.id.show_button: {
            adLoader.showAd();
            break;
        }
    }
}
    

Kotlin:

override fun onClick(v: View) {
    when (v.id) {
        R.id.load_button -> {
            adOverlay.loadAd()
        }
        R.id.show_button -> {
            adOverlay.showAd()
        }
    }
}
    

Like in normal view, interstitial also has events that need to be called from outside. Those events are onResume, onPause, onSaveInstanceState, onRestoreInstanceState, destroy. With these methods you assure that ad is loaded properly and its instance is saved and restored whenever device screen rotates.

Java:

@Override
protected void onResume() {
    super.onResume();
    adOverlay.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    adOverlay.onPause();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    adOverlay.destroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (adOverlay != null)
        adOverlay.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (adOverlay != null)
        adOverlay.onRestoreInstanceState(savedInstanceState);
}
    

Kotlin:

override fun onResume() {
    super.onResume()        
    adOverlay.onResume()
}

override fun onPause() {
    super.onPause()
    adOverlay.onPause()
}

override fun onDestroy() {
    super.onDestroy()
    adOverlay.destroy()
}

public override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    adOverlay.onSaveInstanceState(outState)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    adOverlay.onRestoreInstanceState(savedInstanceState)
}
    

Note that Fragment has slightly different methods than Activity, so if you are implementing code on Fragment, it should be public void onResume(), public void onPause(), public void onDestroy(), public void onSaveInstanceState(Bundle outState), public void onViewStateRestored(Bundle savedInstanceState).

Custom ad size

  • To set ad size you can use

Java:

adOverlay.setAdSize(new AdSize(320, 480));

Kotlin:

adOverlay.adSize = AdSize(320, 480)
  • If you want to support multiple ad sizes at the same placement without setting them, you could use additional dimensions feature.

Java:

adOverlay.setEnabledAdditionalDimensions(true);

Kotlin:

adOverlay.enabledAdditionalDimensions = true
  • In order to set multiple ad sizes you can use

Java:

adOverlay.setSupportedAdSizes(new AdSize(320, 480), new AdSize(300, 300));

Kotlin:

adOverlay.setSupportedAdSizes(AdSize(320, 480), AdSize(300, 300))
Clone this wiki locally