AdMob in Android

AdMob in Android

 
 

App prerequisites

  • Make sure that your app's build file uses the following values:
    • Minimum SDK version of 19 or higher
    • Compile SDK version of 33 or higher
 
Add the dependencies for the Google Mobile Ads SDK to your module's app-level Gradle file, normally app/build.gradle:
dependencies {   implementation ("com.google.android.gms:play-services-ads:22.6.0") }
 
  • To do so, add a <meta-data> tag with android:name="com.google.android.gms.ads.APPLICATION_ID".
  • You can find your app ID in the AdMob web interface. For android:value, insert your own AdMob app ID, surrounded by quotation marks.
 
<manifest>   <application> <!-- Below this is test id replace for value tag--> <!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->     <meta-data         android:name="com.google.android.gms.ads.APPLICATION_ID"         android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>   </application> </manifest>
  • In a real app, replace the sample app ID with your actual AdMob app ID. You can use the sample ID if you're just experimenting with the SDK in a Hello World app.
  • Also, note that failure to add the <meta-data> tag exactly as shown results in a crash with the message:
The Google Mobile Ads SDK was initialized incorrectly.
(Optional) Declare AD_ID permission for previous versions to work with Android 13.
 

Initialize the Google Mobile Ads SDK

Before loading ads, have your app initialize the Google Mobile Ads SDK by calling MobileAds.initialize()
  • which initializes the SDK and calls back a completion listener once initialization is complete, or after a 30-second timeout. This needs to be done only once, ideally at app launch.
 
Here's an example of how to call the initialize() method in an Activity:

Example MainActivity (excerpt)

import com.google.android.gms.ads.MobileAds class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main) // To Intialize AdMob         MobileAds.initialize(this) {}     } }
 

Select an ad format

The Google Mobile Ads SDK is now imported and you're ready to implement an ad. AdMob offers a number of different ad formats, so you can choose the one that best fits your app's user experience.

Banner

notion image
  • Rectangular ads that appear at the top or bottom of the device screen.
  • Banner ads stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you're new to mobile advertising, they're a great place to start.
How to Add Banner Ad In your App
How to Add Banner Ad In your App
Documentation: Implement banner ads

Interstitial

notion image
  • Full-screen ads that cover the interface of an app until closed by the user.
  • They're best used at natural pauses in the flow of an app's execution, such as between levels of a game or just after a task is completed.
How to Add Interstitial ads
How to Add Interstitial ads

Native

notion image
  • Customizable ads that match the look and feel of your app.
  • You decide how and where they're placed, so the layout is more consistent with your app's design.
How to Add Native ads
How to Add Native ads
Documentation : Implement native ads

Rewarded

notion image
  • Ads that reward users for watching short videos or interacting with playable ads and surveys. Used for monetizing free-to-play apps.
How to Add Rewarded Ads
How to Add Rewarded Ads
Documentation : Implement rewarded ads
 
 
 

Show Ads in RecyclerView

 
 
 

Documentation