Rectangle Banners will be rendered using MoPubView through the HyBidHeaderBiddingMRectCustomEvent 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 MoPubView inside your layout file.

<com.mopub.mobileads.MoPubView
    android:id="@+id/mopub_mrect"
    android:layout_width="300dp"
    android:layout_height="250dp" />

Create a MoPub view attribute to hold the reference to the UI element.

private MoPubView mMopubMRect;

Get a reference to it from your code.

mMopubMRect = findViewById(R.id.mopub_mrect);
mMopubMRect.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) {

    }
});
mMopubMRect.setAutorefreshEnabled(false);

Create HyBid Ad Request

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

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

        @Override
        public void onRequestFail(Throwable throwable) {

        }
    });

    mRectRequestManager.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.

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

    @Override
    public void onRequestFail(Throwable throwable) {
       // Request ad to MoPub without adding the pre bid keywords
        mMopubMRect.setAdUnitId("MOPUB_AD_UNIT_ID");
        mMopubMRect.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 HyBidHeaderBiddingMRectCustomEvent adapter will be called to render the ad.