Android 네이티브 광고
네이티브 광고는 앱의 디자인과 자연스럽게 어울리는 맞춤형 광고입니다. 광고의 각 요소(제목, 본문, 이미지 등)를 개별적으로 받아 앱의 UI에 맞게 배치할 수 있습니다. 이 가이드에서는 Adrop 네이티브 광고를 Android 앱에 통합하는 방법을 설명합니다.
시작하기 전에
1
2
레이아웃 구성
<!-- res/layout/layout_native_ad.xml -->
<?xml version="1.0" encoding="utf-8"?>
<io.adrop.ads.nativead.AdropNativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/native_ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 광고주 정보 영역 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<!-- 프로필 로고 -->
<ImageView
android:id="@+id/ad_profile_logo"
android:layout_width="40dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginStart="8dp"
android:orientation="vertical">
<!-- 광고주 이름 -->
<TextView
android:id="@+id/ad_advertiser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<!-- 헤드라인 -->
<TextView
android:id="@+id/ad_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<!-- 미디어 콘텐츠 (이미지/비디오) -->
<io.adrop.ads.nativead.AdropMediaView
android:id="@+id/ad_media"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="12dp" />
<!-- 본문 -->
<TextView
android:id="@+id/ad_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
<!-- CTA 버튼 -->
<TextView
android:id="@+id/ad_call_to_action"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="12dp"
android:background="@color/purple_500"
android:gravity="center"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
</io.adrop.ads.nativead.AdropNativeAdView>레이아웃 요소 설명
요소
뷰 타입
필수
설명
3
네이티브 광고 로드
import io.adrop.ads.model.AdropAds
import io.adrop.ads.nativead.AdropNativeAd
import io.adrop.ads.nativead.AdropNativeAdListener
import io.adrop.ads.model.AdropErrorCode
class NativeAdActivity : AppCompatActivity() {
private var nativeAd: AdropNativeAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_native_ad)
// 네이티브 광고 생성
nativeAd = AdropNativeAd(this, AdropAds.PUBLIC_TEST_UNIT_ID_NATIVE)
// 리스너 설정
nativeAd?.listener = object : AdropNativeAdListener {
override fun onAdReceived(ad: AdropNativeAd) {
// 광고 로드 성공 → 뷰에 표시
displayNativeAd(ad)
}
override fun onAdFailedToReceive(ad: AdropNativeAd, errorCode: AdropErrorCode) {
// 광고 로드 실패
Log.e("Adrop", "광고 로드 실패: $errorCode")
}
override fun onAdClicked(ad: AdropNativeAd) {
// 광고 클릭됨
}
override fun onAdImpression(ad: AdropNativeAd) {
// 광고 노출됨
}
}
// 광고 로드
nativeAd?.load()
}
override fun onDestroy() {
super.onDestroy()
nativeAd?.destroy()
}
}4
광고 데이터를 뷰에 연결
private fun displayNativeAd(ad: AdropNativeAd) {
// 레이아웃 inflate
val adView = findViewById<AdropNativeAdView>(R.id.native_ad_view)
// 각 뷰 찾기
val profileLogoView = adView.findViewById<ImageView>(R.id.ad_profile_logo)
val advertiserView = adView.findViewById<TextView>(R.id.ad_advertiser)
val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
val mediaView = adView.findViewById<AdropMediaView>(R.id.ad_media)
val bodyView = adView.findViewById<TextView>(R.id.ad_body)
val ctaView = adView.findViewById<TextView>(R.id.ad_call_to_action)
// 광고 데이터 설정
ad.profile.displayLogo?.let { profileLogoView.setImageDrawable(it) }
advertiserView.text = ad.advertiser
headlineView.text = ad.headline
bodyView.text = ad.body
ctaView.text = ad.callToAction
// AdropNativeAdView에 뷰 등록
adView.setProfileLogoView(profileLogoView)
adView.setAdvertiserView(advertiserView)
adView.setHeadlineView(headlineView)
adView.setMediaView(mediaView)
adView.setBodyView(bodyView)
adView.setCallToActionView(ctaView)
// 광고 객체 연결 (반드시 마지막에 호출)
adView.setNativeAd(ad)
// 컨테이너에 추가
val container = findViewById<FrameLayout>(R.id.native_ad_container)
container.removeAllViews()
container.addView(adView)
}광고 데이터 속성
속성
타입
설명
5
리소스 해제
override fun onDestroy() {
super.onDestroy()
// 광고 객체 정리
nativeAd?.destroy()
// AdropNativeAdView 정리
val adView = findViewById<AdropNativeAdView>(R.id.native_ad_view)
adView?.destroy()
}@Override
protected void onDestroy() {
super.onDestroy();
// 광고 객체 정리
if (nativeAd != null) {
nativeAd.destroy();
}
// AdropNativeAdView 정리
AdropNativeAdView adView = findViewById(R.id.native_ad_view);
if (adView != null) {
adView.destroy();
}
}6
전체 코드 예시
import android.os.Bundle
import android.util.Log
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import io.adrop.ads.model.AdropAds
import io.adrop.ads.model.AdropErrorCode
import io.adrop.ads.nativead.AdropMediaView
import io.adrop.ads.nativead.AdropNativeAd
import io.adrop.ads.nativead.AdropNativeAdListener
import io.adrop.ads.nativead.AdropNativeAdView
class NativeAdActivity : AppCompatActivity() {
private var nativeAd: AdropNativeAd? = null
private lateinit var nativeAdView: AdropNativeAdView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_native_ad)
nativeAdView = findViewById<AdropNativeAdView>(R.id.native_ad_view)
loadNativeAd()
}
private fun loadNativeAd() {
nativeAd = AdropNativeAd(this, AdropAds.PUBLIC_TEST_UNIT_ID_NATIVE).apply {
listener = object : AdropNativeAdListener {
override fun onAdReceived(ad: AdropNativeAd) {
Log.d("Adrop", "네이티브 광고 로드 성공")
displayNativeAd(ad)
}
override fun onAdFailedToReceive(ad: AdropNativeAd, errorCode: AdropErrorCode) {
Log.e("Adrop", "네이티브 광고 로드 실패: $errorCode")
}
override fun onAdClicked(ad: AdropNativeAd) {
Log.d("Adrop", "네이티브 광고 클릭됨")
}
override fun onAdImpression(ad: AdropNativeAd) {
Log.d("Adrop", "네이티브 광고 노출됨")
}
}
}
nativeAd?.load()
}
private fun displayNativeAd(ad: AdropNativeAd) {
nativeAdView.apply {
// 뷰 찾기
val profileLogoView = findViewById<ImageView>(R.id.ad_profile_logo)
val advertiserView = findViewById<TextView>(R.id.ad_advertiser)
val headlineView = findViewById<TextView>(R.id.ad_headline)
val mediaView = findViewById<AdropMediaView>(R.id.ad_media)
val bodyView = findViewById<TextView>(R.id.ad_body)
val ctaView = findViewById<TextView>(R.id.ad_call_to_action)
// 광고 데이터 설정
Glide.with(this).load(ad.profile.displayLogo).into(profileLogoView)
advertiserView.text = ad.profile.displayName
headlineView.text = ad.headline
bodyView.text = ad.body
ctaView.text = ad.callToAction
// 뷰 등록
setProfileLogoView(profileLogoView)
setAdvertiserView(advertiserView)
setHeadlineView(headlineView)
setMediaView(mediaView)
setBodyView(bodyView)
setCallToActionView(ctaView)
// 광고 연결
setNativeAd(ad)
}
}
override fun onDestroy() {
super.onDestroy()
nativeAd?.destroy()
nativeAdView?.destroy()
}
}추가 옵션
전체 영역 클릭 활성화
커스텀 클릭 처리
커스텀 필드 사용

Last updated