RoadMap : Admob, Getting Started > Android 요구사항 > Banners 1

Menus : 실습




https://developers.google.com/mobile-ads-sdk/docs/ 주소에 접속하시면, 구글 애드몹 SDK 관련 문서를 보실 수 있고, 그 속에는 몇몇 예제들이 있습니다. 본 페이지는 그 중 'Banners I' 이라는 예제를 번역하고 따라 해본 페이지 입니다. 예제의 원문서는 https://developers.google.com/mobile-ads-sdk/docs/ 접속 후 좌측 메뉴 'AdMob > Banner Ads I'을 선택하면, 보실 수 있습니다.




선행 작업.

- Android AdMob 요구사항 진행 : http://it-backup.tistory.com/10





본격 적인 번역.


Banners I

Google AdMob Ads banners use a small portion of the screen to entice users to "click through" to a richer, full-screen experience such as a website or app store page. This guide shows you how to enable your app to serve a banner ad.
구글 애드몹 광고 배너들은 화면의 작은 부분을 사용해 유저가 클릭을 통해 richer(글꼴의 크기 변경 및 이미지 표현이 가능한 문서), 모든 화면을 통해 웹사이트나 앱스토어 페이지를 경함하게끔 유혹합니다. 이 가이드는 앱에 배너 광고 제공 방법을 보여줍니다.


To display banners in your Android app, simply add a com.google.ads.AdView to your UI.
앱속 배너 배너를 표시하려면, 간단히 UI에 'com.google.ads.AdView'추가합니다.

Adding a com.google.ads.AdView

Android apps are composed of View objects, Java instances the user sees as text areas, buttons and other controls. AdView is simply another View subclass displaying small HTML5 ads that respond to user touch.
안드로이드 앱은 뷰 객체들, 유저가 보는 텍스트 영역의 자바 인스턴스, 버튼과 기타 컨트롤로 구성되어 있습니다. AdView는 사용자 터치에 반응(응답)하는 작은 HTML5 광고를 표시하는 또 다른 뷰의 subclass 뷰 입니다.

Like any View, an AdView may be created either purely in code or largely in XML.
다른 뷰와 같이, 순수한 코드나 주로 사용되는 XML로 AdView 생성할 수 있습니다.

The five lines of code it takes to add a banner:
배너를 추가하는데 이용되는 5줄의 코드 :

  • Import com.google.ads.*
    com.google.ads.* (패키지) 추가

  • Declare an AdView instance
    AdView 인스턴스 선언.

  • Create it, specifying a unit ID—your AdMob publisher ID
    장치 아이디 (당신의 AdMob 게시자 ID), 생성.

  • Add the view to the UI
    UI속에 view(AdView) 추가.

  • Load it with an ad
    광고 로드.

The easiest place to do all this is in your app's Activity.
아래 위치는 당신의 앱 Activity속 코드를 추가하기 좋은 장소 입니다.

import com.google.ads.*; // 패키지 추가.

public class BannerExample extends Activity {
 
private AdView adView;                            // adView 선언.

 
@Override
 
public void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView
(R.layout.main);

   
// Create the adView; adView 추가.
    adView
= new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);

   
// Lookup your LinearLayout assuming it's been given
   
// the attribute android:id="@+id/mainLayout" // "@+id/mainLayout" 속성 ID의 LinearLayout을 조회하는 것을 가정하고 있다.

    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

   
// Add the adView to it.                위에서 조회된 LinearLayout 에 adView를 추가한다.
    layout
.addView(adView);

   
// Initiate a generic request to load it with an ad.            광고와 adView 응답의 초기화를 시작한다.
    adView
.loadAd(new AdRequest());
 
}

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

You can download an example project containing this code here.
당신은 위 코드가 포함된 예제 프로젝트를 여기(here)에서 다운로드 할 수 있습니다.



Create your banner in XML. (XML 속에서 배너 만들기)

Rather than creating your AdView in Java, it's also possible to set one up entirely in XML. To do so simply:
(당신이 자바속에서 AdView를 만드는 것 보다, 한번에 쉽게 XML 속에서 설정하는 것이 가능합니다. 훨씬 간결하게 :

  • Incorporate the SDK into your app
    당신의 앱속에 SDK를 통합합니다.

  • Define a com.google.ads.AdView in res/layout/main.xml, specifying it should immediately load an ad by using the ads:loadAdOnCreate attribute.
    'res/layout/main.xml' 속에 com.google.ads.AdView를 정의합니다. 'ads:loadAdOnCreate' 속성을 이용해 정의된 AdView에 빠르게 광고가 로드됩니다.

  • Alternately, instead of forcing the AdView to load an ad immediately, Look up the AdView as a resource at runtime and tell it to request an ad.
    또는 강제로 AdView에 바로 로드되는 대신, 런타임과 광고를 요청하는 시점에 리소스를 조회해 찾은 AdView에 보여지게 할 수 있습니다.

Defining a com.google.ads.AdView (com.google.ads.AdView 정의)

The easiest way to incorporate an ad is to simply define your AdView as you would any other part of yourres/layout/main.xml:

광고를 통합하는 쉬운 방법은 광고뷰를 'res/layout/main.xml'에 정의하는 간단히 정의하는 것이다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
             
android:orientation="vertical"
             
android:layout_width="fill_parent"
             
android:layout_height="fill_parent">
 
<com.google.ads.AdView android:id="@+id/adView"
                         
android:layout_width="wrap_content"
                         
android:layout_height="wrap_content"
                         
ads:adUnitId="MY_AD_UNIT_ID"
                         
ads:adSize="BANNER"
                         
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                         
ads:loadAdOnCreate="true"/>
</LinearLayout>

As usual you must replace MY_AD_UNIT_ID with your AdMob publisher ID. You must also add your own device ID in theads:testDevices attribute to get test ads on your device. Note the inclusion of the ads namespace referenced in specifyingadUnitId and adSize. This code will immediately attempt to load an ad as soon as the AdView is created by Android's layout engine.

 하던데로, MY_AD_UNIT_ID와 당신의 Admob publisher ID (애드몹 게시자 ID)를 치환하고 합니다. 또한 당신의 장치에서 테스트 광고를 얻기 위해 'ads:testDevices' 속성속에 자신의  장비 ID를 추가해야 합니다. 지정된 'adUnitId'와 'adSize'가 ads 네임스페이스에 포함된 되었는지 확인하세요. 이코드는 안드로이드 레이아웃 엔진에 의해 생성되는 즉시 AdView에 광고를 로드합니다.



Lookup and Load (조회와 로드)

If you have a need to control the AdRequest used to load an ad in your application, you can remove theads:loadAdOnCreate="true" line from the above code. Instead, you will want to look up the AdView as a resource viafindViewById and instruct it to loadAd:

만약 당신이 app속에서 AdRequest를 이용한 광고 로드 제어가 필요하다면, 위의 코드로 부터 'ads:loadAdOnCreate="true"' 라인을 제거할 수 있습니다. 대신, 'findViewById'를 통해 리소스에서 조회된 AdView에 'loadAd'(메소드)로 지시합니다.

import com.google.ads.*;

public class BannerExample extends Activity {
 
@Override
 
public void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView
(R.layout.main);

   
// Look up the AdView as a resource and load a request.
   
AdView adView = (AdView)this.findViewById(R.id.adView);
    adView
.loadAd(new AdRequest());
 
}
}

You can download an example project containing this code here.
이 코드가 들어 있는 예제 프로젝트는 이곳(here)에서 다운로드 할 수 있습니다.


The Result

When you now run your app you should see a banner at the top of the screen:


Posted by 창업자닉군
,