Skip to content

Integrating Interstitial Ad

rprunskas edited this page May 31, 2022 · 7 revisions

AdInterstitial is an ad that can be shown when using ViewPager class. It supports various custom user implementations, as it works 'on top' of ViewPager view.

To show how to implement this functionality, we need to set up basic ViewPager implementation. If you already have this functionality, you can skip this step.

Implementing ViewPager (optional)

To use ViewPager we need to position it in the layout XML file.

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
            

Then we need to create a fragment, that will be used for swiping. For this purpose we will be using a simple EmptyFragment with a text inside, that shows the page number.

public class EmptyFragment extends Fragment {
    public static final String INSTANCE_TEXT = "TEXT";
    private String text;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_empty, container, false);
        if (savedInstanceState != null)
            text = savedInstanceState.getString(INSTANCE_TEXT);
        if (text != null)
            ((TextView) rootView.findViewById(R.id.text_view)).setText(text);
        return rootView;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(INSTANCE_TEXT, text);
    }
}
    

And use it with adapter implementation.

private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object2) {
        super.destroyItem(container, position, object2);
    }

    @Override
    public Fragment getItem(int position) {
        EmptyFragment emptyFragment = new EmptyFragment();
        emptyFragment.setText(texts.get(position));
        return emptyFragment;
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}   
    

By running this code, you'll be having a basic ViewPager implementation with 30 'pages' of EmptyFragment's.

Implementing AdInterstitial

For the AdInterstitial to work with the pager adapter, we need to wrap view pager in AdInterstitial container in XML layout file.

We will customize basic ViewPager a bit for it to work properly with the AdInterstitial. Basicly what ViewPager need is to implement PageableAdHandler interface. This is already done for your convenience, so instead of using a default ViewPager, we will be using BasicInterstitialPager or WebviewInterstitialPager if pager has items with webview content.

For more information, how to make such implementation, please look into Advanced documentation.

    <com.adform.sdk.pager.AdInterstitial
        android:id="@+id/pager_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.adform.sdk.pager.BasicInterstitialPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </com.adform.sdk.pager.AdInterstitial>    

Further implementation is more generic one, as it is not that different from AdInline or AdOverlay. First we get the AdInterstitial instance.

Java:

private AdInterstitial adInterstitial;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_adinterstitial);
    adInterstitial = (AdInterstitial) findViewById(R.id.pager_container);
    adInterstitial.setMasterTagId(12345);
}    

Kotlin:

private lateinit var adInterstitial: AdInterstitial

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_adinterstitial)
    adInterstitial = findViewById(R.id.pager_container)
    adInterstitial.setMasterTagId(12345)
}    

Provide it with mandatory world events onResume, onPause, onDestroy

Java:

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

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

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

Kotlin:

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

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

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

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().

And that is it. It automatically loads and shows the ad whenever ad loads successfully and page change has reaches the limit (by default 3 page changes).

Custom ad size

  • To set ad size you can use

Java:

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

Kotlin:

adInterstitial.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:

adInterstitial.setEnabledAdditionalDimensions(true);

Kotlin:

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

Java:

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

Kotlin:

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