문서가 업데이트되었습니다.
한국어로 번역이 아직 완료되지 않았습니다.
영어 업데이트됨: 11월 11일

Add Banner and Medium Rectangle Ads to an Android App

The Audience Network allows you to monetize your Android apps with Facebook ads. This guide explains how to add banner and medium rectangle ads to your app.

You can change placements in Monetization Manager to the Medium Rectangle format if these were previously configured as Banner for bidding. Similarly, for any new medium rectangle placements, navigate to the placement settings page in Monetization Manager and select Medium Rectangle (not Banner).

Placements will deliver as normal even if they are not changed to the medium rectangle format. However, to avoid confusion, we recommend that you change these placements to medium rectangle.

If you're interested in other kinds of ad units, see the list of available types.

Ensure you have completed the Android Setup Guides guides before you proceed.

Banner and Medium Rectangle Ad Steps

Step 1: Adding a Layout Container for the Banner Ad

Step 2: Implementing the Banner in your Activity

Step 3: Adding an Ad Listener

Step 1: Adding a Layout Container for the Banner Ad

In your layout file (for example: /res/layout/activity_main.xml), add a layout that will act as a container for your Ad.
Remember the id you set here as you will be referencing it in the code later.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
...
>
...
<LinearLayout
android:id="@+id/banner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
/>
...
</RelativeLayout>

Step 2: Implementing the Banner in your Activity

Add the following code at the top of your Activity in order to import the Facebook Ads SDK:

import com.facebook.ads.*;

Next, instantiate an AdView object and make a request to load an ad. Since AdView is a subclass of View, you can add it to your view hierarchy just as with any other view:

private AdView adView;

@Override
public void onCreate(Bundle savedInstanceState) {
...
// Instantiate an AdView object. 
// NOTE: The placement ID from the Facebook Monetization Manager identifies your App.
// To get test ads, add IMG_16_9_APP_INSTALL# to your placement id. Remove this when your app is ready to serve real ads.

adView = new AdView(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_50);

// Find the Ad Container
LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);

// Add the ad view to your activity layout
adContainer.addView(adView);

// Request an ad
adView.loadAd();
}

If you are building your app for tablet, consider using the AdSize.BANNER_HEIGHT_90 size instead. In all cases, the banner width is flexible with a minimum of 320px.

Lastly, add the following code to your activity's onDestroy() function to release resources the AdView uses:

@Override
protected void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}

Once you run the above, you should see something like this:



기본 Google Android 에뮬레이터를 사용하는 경우 테스트 광고를 읽어들이기 전에 다음 코드 행을 추가합니다.
AdSettings.addTestDevice("HASHED ID");.

기기에 광고를 읽어들이기를 처음 요청할 때는 logcat에 인쇄되어 있는 해시 처리된 ID를 사용하세요.

Genymotion 및 실제 기기에서는 이 단계가 필요하지 않습니다. 실제 광고를 사용하여 테스트하려면 테스트 가이드를 참조하세요.

Step 3: Adding an Ad Listener

Now that you have the basic code running, you can set an AdListener to your AdView to listen for specific events:

import android.widget.Toast;
...

protected void onCreate(Bundle savedInstanceState) {
...
AdListener adListener = new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
// Ad error callback
Toast.makeText(
MainActivity.this,
"Error: " + adError.getErrorMessage(),
Toast.LENGTH_LONG)
.show();
}

@Override
public void onAdLoaded(Ad ad) {
// Ad loaded callback
}

@Override
public void onAdClicked(Ad ad) {
// Ad clicked callback
}

@Override
public void onLoggingImpression(Ad ad) {
// Ad impression logged callback
// Please refer to Monetization Manager or Reporting API for final impression numbers
}
};

// Request an ad
adView.loadAd(adView.buildLoadAdConfig().withAdListener(adListener).build());
}

Ad Banner Sizes

Audience Network supports three ad sizes to be used in your AdView. The Banner unit's width is flexible with a minimum of 320px, and only the height is defined.

Ad Format AdSize Reference Size Recommendation

Standard Banner

BANNER_50

320x50

This banner is best suited to phones

Large Banner

BANNER_90

320x90

This banner is best suited to tablets and larger devices

Medium Rectangle

RECTANGLE_HEIGHT_250

300x250

This format is best suited for scrollable feeds or end-of-level screens

Next Steps