# 배너 광고

### 1 : 준비

* [프로젝트 설정](/adpie/android/project-settings.md)을 통해 광고 연동을 위한 준비를 합니다.

### 2 : 레이아웃 XML에 슬롯 지정

* [사이트](http://my.adpies.com)에서 발급받은 Slot ID를 XML 또는 자바코드에서 필수로 설정해주어야 합니다.
* 광고뷰가 사이트에서 설정한 슬롯 사이즈보다 작은 경우 요청하지 않거나 표출하지 않습니다.

```xml
<com.gomfactory.adpie.sdk.AdView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ad_view"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    slotID="AdPie-Slot-ID" >
</com.gomfactory.adpie.sdk.AdView>
```

### 3 : 광고 요청과 표출

* SDK 초기화를 하지 않았다면, [사이트](http://my.adpies.com)에서 발급받은 Media ID를 입력하여 초기화를 호출해야 합니다.
* Step 2에서 xml 광고뷰에 Slot ID 를 넣지 않았다면, `setSlotId("AdPie-Slot-ID")` 메소드를 통해 입력합니다.
* `setScaleUp(true)`를 통해 정해진 영역내에서 비율을 유지하여 최대 2배까지 확대 가능합니다. (세로모드 기준으로 가로모드 제외)
* 띠배너에 `setScaleUp(true)`을 적용하면 스마트폰 기준으로 너비가 꽉채워지며 320x50 광고의 경우 높이는 50dp \~ 100dp가 됩니다.
* 광고 컨텐츠에 맞게 광고뷰 좌우여백의 색상을 채우고자 한다면 `setAutoBgColor(true)`메소드를 호출합니다. (스케일업 배너 제외)

```java
private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // SDK 초기화로 앱실행 1회만 호출해야 합니다. (Main Activity 에 필수로 입력합니다.)
    AdPieSDK.getInstance().initialize(getApplicationContext(), "AdPie-Media-ID");

    ...

    // 광고 연동을 위한 슬롯 ID를 필수로 입력합니다.
    adView = (AdView) findViewById(R.id.ad_view);
    // adView.setSlotId("AdPie-Slot-ID");
    
    /** Optional
    // 스케일배너 적용 : 기본값은 false
    adView.setScaleUp(true);
    // 자동 배경색상 적용 : 기본값은 false (스케일배너 제외)
    adView.setAutoBgColor(true);
    **/

    adView.load();
}

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

    super.onDestroy();
}
```

### 4 : 광고 리스너 사용

* 광고 요청에 대한 이벤트를 수신할 수 있습니다.
* [에러코드](/adpie/android/common/errorcode.md)를 통해 광고 실패에 대한 이유를 알 수 있습니다.

```java
adView.setAdListener(new com.gomfactory.adpie.sdk.AdView.AdListener() {
        @Override
        public void onAdLoaded() {
                // 광고 표출 성공 후 이벤트 발생
        }
        
        @Override
        public void onAdFailedToLoad(int errorCode) {
                // 광고 요청 또는 표출 실패 후 이벤트 발생
                // error message -> AdPieError.getMessage(errorCode)
        }
        
        @Override
        public void onAdClicked() {
                // 광고 클릭 후 이벤트 발생
        }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://platform-business.gitbook.io/adpie/android/integration/banner.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
