Google AdMob is a mobile advertising platform that can be integrated into Android application. It allows monetizing application by displaying ads alongside app content. Ads are automatically served from the AdMob Network and third-party networks.
The Google Mobile Ads SDK is a part of the Google Play Services. So, we need to add play-services-ads
dependency in the module's build.gradle
file.
dependencies {
// Other dependencies
// ...
implementation 'com.google.android.gms:play-services-ads:19.3.0'
}
To use the Google AdMob, we need an app ID and ad unit ID.
- An app ID is a unique identifier that is assigned to an application when it added to AdMob. ID is used to identify application.
- An ad unit ID is a unique identifier that is assigned to the ad unit when it created in AdMob. ID is used to identify requests from the ad unit.
We can get these identifiers by following these steps:
- Login to the AdMob Account.
- On the left side of the dashboard, click "Apps".
- Click "ADD YOUR FIRST APP".
- Provide app name, choose Android platform, and then click "ADD".
- Copy app ID.
- Open the
AndroidManifest.xml
file and add themeta-data
element which defines an app ID.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<application>
<!-- ... -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXXXX"/>
<activity android:name=".MainActivity">
<!-- ... -->
</activity>
</application>
</manifest>
- In the AdMob dashboard, click "NEXT: CREATE AD UNIT".
- Select "BANER".
- Provide ad unit name, and then click "CREATE AD UNIT".
- Copy ad unit ID.
- In the layout XML file, add
AdView
element. By using theads:adUnitId
attribute, define the ad unit ID. Theads:adSize
attribute defines ad size.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.gms.ads.AdView
android:id="@+id/myAdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXXXX" />
</RelativeLayout>
- On the left side of the AdMob dashboard, click "Payments".
- Provide required information. A payment method don't needed to add until earnings threshold has been reached.
The Mobile Ads SDK is initialized by using MobileAds.initialize()
method. The loadAd()
method is used to load an ad. The AdRequest
is passed as parameter, which contains information about ad request.
package com.example.app
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.android.gms.ads.*
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
MobileAds.initialize(this)
val configuration = RequestConfiguration.Builder()
.setTestDeviceIds(listOf("7C4136XXXXXXXXXXXXXXXXXXXXXXXXXX"))
.build()
MobileAds.setRequestConfiguration(configuration)
val adRequest = AdRequest.Builder().build()
myAdView.loadAd(adRequest)
}
}
When building and testing an application, we need to configure the device as a test device, otherwise AdMob account can be suspended. We need to get the device ID. To do that, run the application and check the logcat output for a message:
I/Ads: Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("7C4136XXXXXXXXXXXXXXXXXXXXXXXXXX") to get test ads on this device.
Leave a Comment
Cancel reply