MoPub Banners - pubnative/pubnative-hybid-android-sdk GitHub Wiki

Banners will be rendered using MoPubView through the HyBidHeaderBiddingBannerCustomEvent adapter.

Requirements:

  • Ad Zone Id from the PubNative Publisher Dashboard
  • MoPub Ad Unit Id for the ad placement that you want to request.

Code sample

You can find a demo app with code samples for this type of integration here.

Create XML layout

Create a MoPub view inside your layout file.

<com.mopub.mobileads.MoPubView
    android:id="@+id/mopub_banner"
    android:layout_width="320dp"
    android:layout_height="50dp" />

Create a MoPubView attribute to hold the reference to the UI element.

private MoPubView mMopubBanner;

Get a reference to it from your code.

mMopubBanner = findViewById(R.id.mopub_banner);
mMopubBanner.setBannerAdListener(new MoPubView.BannerAdListener() {
    @Override
    public void onBannerLoaded(MoPubView banner) {
                
    }

    @Override
    public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode) {

    }

    @Override
    public void onBannerClicked(MoPubView banner) {

    }

    @Override
    public void onBannerExpanded(MoPubView banner) {

    }

    @Override
    public void onBannerCollapsed(MoPubView banner) {

    }
});
mMopubBanner.setAutorefreshEnabled(false);

Create HyBid Ad Request

Create a RequestManager to handle the request to the ad server.

private void loadBanner() {
    RequestManager bannerRequestManager = new BannerRequestManager();
    bannerRequestManager.setZoneId("ZONE_ID");
    bannerRequestManager.setRequestListener(new RequestManager.RequestListener() {
        @Override
        public void onRequestSuccess(Ad ad) {
            // Here you will handle the request to MoPub. 
        }

        @Override
        public void onRequestFail(Throwable throwable) {

        }
    });

    bannerRequestManager.requestAd();
}

Request ad to MoPub

After the ad is successfully received from PubNative, the request should be made to MoPub with some parameters that will help the ad be chosen properly in the MoPub waterfall.

The HeaderBiddingUtils must be used so the SDK can generate the proper keywords for the received ad.

bannerRequestManager.setRequestListener(new RequestManager.RequestListener() {
    @Override
    public void onRequestSuccess(Ad ad) {
        mMopubBanner.setAdUnitId("MOPUB_AD_UNIT_ID");
        mMopubBanner.setKeywords(HeaderBiddingUtils.getHeaderBiddingKeywords(ad, KeywordMode.TWO_DECIMALS));
        mMopubBanner.loadAd();
    }

    @Override
    public void onRequestFail(Throwable throwable) {
        // Request ad to MoPub without adding the pre bid keywords
        mMopubBanner.setAdUnitId("MOPUB_AD_UNIT_ID");
        mMopubBanner.loadAd();
    }
});

After making this request to MoPub, it will run it's waterfall and if the line item targeted by our keywords gets chosen, the HyBidHeaderBiddingBannerCustomEvent adapter will be called to render the ad.


-> Rectangle Banners