Integrating Banner Ads - Android


Before integrating banner ads in your app, you’ll need to go through the steps in our Getting Started Guide to create an account on MoPub and integrate the SDK into your project.

Sample code:

For a complete example, check out the mopub-sample

Load banner ads in your app

1. Check your AndroidManifest.xml file

Be sure you have the following permissions:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

`ACCESS_COARSE_LOCATION` or `ACCESS_FINE_LOCATION` are only needed if you want the device to automatically send the user’s location for targeting.

If your app collects location from the user, we recommend passing it up, as impressions with location earn higher revenue. `WRITE_EXTERNAL_STORAGE` is optional and only required for `MRAID 2.0` store picture ads. You’ll also need to include the following custom Activities (even though you won’t instantiate them directly):

<activity android:name="com.mopub.mobileads.MoPubActivity" android:configchanges="keyboardHidden|orientation"></activity>
 <activity android:name="com.mopub.mobileads.MraidActivity" android:configchanges="keyboardHidden|orientation"></activity>
 <activity android:name="com.mopub.common.MoPubBrowser" android:configchanges="keyboardHidden|orientation"></activity>
 <activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configchanges="keyboardHidden|orientation"></activity>

2. Define a slot for your banner ad in your layout XML

The MoPub SDK provides a custom View subclass, MoPubView, that handles requesting and loading ads. Start by including this XML block to your Activity’s or Fragment’s layout. The expected dimensions for a banner ad are screen width and 50dp high; these should not be modified.



3. Load an ad into the banner slot

Next, in your Activity or Fragment code, declare an instance variable for your MoPubView:

private MoPubView moPubView;

You should already have created an ad unit on MoPub’s site and received an Ad Unit ID. You’ll use it now to identify that ad unit in your app and request ads from MoPub that are relevant for your users. In your Activity’s `onCreate()` or your Fragment’s `onCreateView()` method, set your `MoPubView`’s Ad Unit ID, then simply call `loadAd()` to fetch and display the ad:

 moPubView = (MoPubView) findViewById(;
 moPubView.setAdUnitId("xxxxxxxxxxx"); // Enter your Ad Unit ID from

When the hosting Activity or Fragment is destroyed, be sure to also destroy the MoPubView by calling:


Using the Listener Interface

`MoPubView` provides a listener interface, `BannerAdListener`, which you can use to stay informed about ad lifecycle events. `BannerAdListener` includes the following methods:

// Sent when the banner has successfully retrieved an ad.
 public void onBannerLoaded(MoPubView banner);
 // Sent when the banner has failed to retrieve an ad. You can use the MoPubErrorCode value to diagnose the cause of failure.
 public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode);
 // Sent when the user has tapped on the banner.
 public void onBannerClicked(MoPubView banner);
 // Sent when the banner has just taken over the screen.
 public void onBannerExpanded(MoPubView banner);
 // Sent when an expanded banner has collapsed back to its original size.
 public void onBannerCollapsed(MoPubView banner);

To listen for events, you’ll need to implement the listener interface:

public class ExampleActivity extends Activity implements BannerAdListener {
     public void onBannerLoaded(MoPubView banner) {
             "Banner successfully loaded.", Toast.LENGTH_SHORT).show();
     // ... other BannerAdListener methods ...

and pass your Activity to the MoPubView as it’s being created (see Step 3, above):


Refreshing Ads

The `MoPubView` will automatically refresh your ad unit at a time interval that you set using the MoPub web interface. You can also programmatically toggle auto-refresh on a particular `MoPubView` using the `setAutorefreshEnabled` method.

Note: Eclipse sometimes adds an `import android.R` statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This may cause your build to break; you can fix this by deleting the import statement.

Updated: November 2017