LoginSignup
2
4

More than 5 years have passed since last update.

[備忘録]androidでのadmobの表示

Last updated at Posted at 2016-11-30

チュートリアルをコピペたして動かなかったのでメモ。

activityのxmlで、チュートリアルは、


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

という感じで、RelativeLayoutのタグにxmlns:adsを記述しているが、
これだと、エラーになる。
ググってみても、

    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

とか、

    xmlns:ads="http://schemas.android.com/apk/lib/アプリのパッケージ名"

とか、出てくるけど、これも、起動時、Viewの部分に、adSizeが記述されてない的なメッセージが出て表示されない。

正解は、「com.google.android.gms.ads.AdView」タグに記述する。

   <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true" />

これだと、表示される。。。
ググってstackoverflowみても、コードで、onCreate時に、
AdViewに、adSizeやunitid設定しろとか書いてるけど、動かない。
(adSize定義してないとか表示される。)

かつ、上記設定をした場合、コードで、unitidを設定すると、
2回unitid設定はエラーになる。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4