/
Android SDK - Ad Integration

Android SDK - Ad Integration

Traditional Formats

 

Traditional formats are generally rectangular and they are inserted into the app’s content. To show this type of ads, you must create and instantiate a “BannerAdView”.

Before creating a “BannerAdView”, it is necessary to import certain controls, as it is shown in the sample code. Then, the “BannerAdView” is created, indicating the placement ID and the selected size.

 

For your reference, please read the following chart of suggested sizes:

 

Smartphones

240x38 / 300x50 / 320x50 / 480x75

Tablets

728x90 / 300x250

 

The next example shows how to implement a 320x50 ad in an Android Application activity:

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.FrameLayout;

import com.appnexus.opensdk.*;

import com.appnexus.opensdk.R.*;

  

public class MainActivity extends Activity {

 

 @Override

 protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 

 // Create the Ad and set up the Placement ID,

 // Configure that it will be a Native Ad (it will run in the App) and its size

 BannerAdView bav = new BannerAdView(this);

 bav.setPlacementID('e41191d9114cfa18');

 bav.setShouldServePSAs(true);

 bav.setOpensNativeBrowser(true);

 bav.setAdSize(320, 50);

 // Add the Ad to the view

 FrameLayout layout = (FrameLayout)findViewById(android.R.id.content);

 layout.addView(bav);

 // Upload the Ad

 bav.loadAd();

 }

 

 @Override

 public boolean onCreateOptionsMenu(Menu menu) {

 getMenuInflater().inflate(R.menu.main, menu);

 return true;

 }

}

 

*Test Gist: https://gist.github.com/emaraschio/0a0b5dc29e6963478ba5

 

Interstitial Formats

Interstitial formats are full screen and they are displayed over the app’s content. They include a solid color background and a close button at the top right corner, which allows the user to close the advertisement and continue navigating the application. To show this type of ads, you must create and instantiate an “InterstitialAdView”.

Before creating an “InterstitialAdView”, it is necessary to import certain controls, as it is shown in the sample code. Then, the “InterstitisalAdView” is created, indicating the placement ID and the selected size.

 

For your reference, please read the following chart of suggested sizes:

Smartphones

300x250 / 320x480

Tablets

900x500 / 1024x1024

 

The next example shows how to implement a 1024x1024 Interstitial ad in an Android Application activity:

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.util.Log;

import com.appnexus.opensdk.*;

  

public class MainActivity extends Activity implements AdListener {

 

 @Override

 protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.activity_main);

 

 // Create the Ad and set up the Placement ID

 InterstitialAdView iav = new InterstitialAdView(this);

 iav.setPlacementID('1326299');

 iav.setAdListener(this);

 // Send the Ad’s request to EPL.

 // If EPL approves the 'onAdLoaded' method, the Ad will be displayed.

 iav.loadAd();

 }

 

 @Override

 public void onAdLoaded(AdView av) {

 Log.d('onAdLoaded', 'The Ad has been loaded - The Ad is displayed');

 // Since the Ad has already been requested to the Ad server and its response was 'OK',

 // The ad is displayed.

 InterstitialAdView iav = (InterstitialAdView) av;

 iav.show();

 }

 

 @Override

 public void onAdRequestFailed(AdView av) {

 Log.d('onAdRequestFailed', 'The request to the Adserver didn’t return a correct answer');

 }

 

 @Override

 public void onAdClicked(AdView av) {

 Log.d('onAdClicked', 'The User clicked on the Ad');

 }

 

 @Override

 public void onAdCollapsed(AdView av) {

 }

 

 @Override

 public void onAdExpanded(AdView av) {

 }

 @Override

 public boolean onCreateOptionsMenu(Menu menu) {

 getMenuInflater().inflate(R.menu.main, menu);

 return true;

*Test Gist: https://gist.github.com/emaraschio/33c51d10de83b78ef674

 

Related content