2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Android】アプリ内にバナー広告を表示【Kotlin】

Last updated at Posted at 2023-06-25

はじめに

今回はアプリ内に表示するバナー広告の表示方法を記事に残します。
 参考資料はこちら↓

実践

layout.xml
    <com.google.android.gms.ads.AdView
        android:id="@+id/banner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
MainActivity.kt
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val banner = findViewById<AdView>(R.id.banner)
        banner.loadAd(AdRequest.Builder().build())
    }

これでアプリ内にバナー広告を表示できるようになります。

注意が2つ

1つ目は
公式にあるこのままの実装だと起動時にアプリがクラッシュしてしまうため以下の二つでクラッシュの対応をしてから 出ないとアプリが開けません。

2つ目は AdRequestのimport時に複数あるので選択を誤るといつまで経ってもエラーが解消できないので気をつけましょう。

その他参考

2
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?