Skip to content

Rendering Native Ads

You can create a ImageView, where you want the ad to be rendered.

For example, if an ad needs to be shown at the spot in the red box

Create an ImageView of the size where the ad needs to be rendered

On this ImageView, assign the rendering script, to render the ad.

Please note that the final design of the ad unit template will be done by the GreedyGame engine.

GreedyGame engine also keeps on optimizing the native ad unit design to improve CTR.

Note

More information about unit design can be found under Best Practices section.

Implementation of Rendering Code

Implement the following custom rendering code to render Native Ads in your ImageView.

Follow the example to do the same.

To fetch the Ad

To fetch the Ad for a unit you need to call getPath(unitId) in GreedyGameAgent instance.

void loadAd(){
    String unitId = "YOUR FLOAT UNIT HERE";
    ImageView iv = (ImageView) findViewById(R.id.your_imageview_id);
    /*
    Setting up click listener for the ad unit. 
    This will launch an intersitial window when user clicks on the ad unit.
    */
    View.OnClickListener unitClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            greedyGameAgent.showUII(unitId); 
        }
    };

    boolean bitmapChanged = false;
    String path = greedyGameAgent.getPath(unitId);
    if (path != null) {
    File file = new File(path);
    if (file.exists()) {
        Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
        if (bm != null) {
            // Use it on your element which needs to be branded.
            bitmapChanged = true;
            iv.setImageBitmap(bm); // This is where you actually set the Native Ad
            iv.setOnClickListener(unitClick);
        }
    }
    }

    if(!bitmapChanged) {
    // Change the asset to original or default texture
    iv.setImageBitmap(originalBitmap);
    iv.setOnClickListener(null);
    }
}

NOTE: Ensure that you mention the correct Unit ID in the highlighted line.

Warning

It's the publisher responsibility to call getPath(unitId) at relevant places to render the ads. For example, in onAvailable() callback of the CampaignStateListener or when you are changing the Activity, calling getPath(unitId) at the start of the activity will help you display the ad correctly

NOTE: When the Campaign state is onUnavailable() or onError(error), ensure that you replace the ad unit by the default texture of your app.