Industrial Training




Google AdMob Banner Ads


In this tutorial, we implement the Google AdMob Banner Ads in our Android application. To place the Google AdMob in Android application, we need to create the Google Ad Unit Id. The complete reference for creating the Google AdMod account and generate the Ad Unit Id is described at Android Google AdMob.
Banner ads are rectangular text or image ads that occupy a small spot in activity layout. To implement the Google AdMob in the Android application, select Google AdMob Ads Activity and select the Ad format type as Banner. This activity adds the default required library dependency, View for Ads display, Internet permission, and other required code.
We are also able to place the Google AdMob Ads on other activities such as Blank Activity.
Add the Google ads dependency 'com.google.android.gms:play-services-ads:17.0.0' in the build.gradle file:


build.gradle


dependencies {  
    implementation fileTree(dir: 'libs', include: ['*.jar'])  
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"  
    implementation 'com.android.support:appcompat-v7:26.1.0'  
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'  
    implementation 'com.google.android.gms:play-services-ads:17.0.0'  
    testImplementation 'junit:junit:4.12'  
}  
dependencies {  
    implementation fileTree(dir: 'libs', include: ['*.jar'])  
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"  
    implementation 'com.android.support:appcompat-v7:26.1.0'  
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'  
    implementation 'com.google.android.gms:play-services-ads:17.0.0'  
    testImplementation 'junit:junit:4.12'  
}  

activity_main.xml


Add the Google Ads View on that layout in which we want to display our ads. Here, we have added in our activity_main.xml file.
To display the banner ad, we need to add the com.google.android.gms.ads.AdView element to our XML layout. The banner ads align to the bottom of the screen.


< ?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="example.javatpoint.com.kotlinbannerads.MainActivity">  
  
    < TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="215dp"  
        android:text="@string/banner_ad_sample"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
    < !-- view for AdMob Banner Ad -->  
    < com.google.android.gms.ads.AdView  
        android:id="@+id/adView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true"  
        ads:adSize="BANNER"  
        ads:adUnitId="@string/banner_ad_unit_id" />  
  
< /RelativeLayout>  
< ?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="example.javatpoint.com.kotlinbannerads.MainActivity">  
  
    < TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="215dp"  
        android:text="@string/banner_ad_sample"  
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>  
  
    < !-- view for AdMob Banner Ad -->  
    < com.google.android.gms.ads.AdView  
        android:id="@+id/adView"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:layout_centerHorizontal="true"  
        ads:adSize="BANNER"  
        ads:adUnitId="@string/banner_ad_unit_id" />  
  
< /RelativeLayout>  

strings.xml


Add the created ad unit id in the string.xml file.


< resources>  
    < string name="app_name">Kotlin Banner Ads  
    < string name="banner_ad_sample">Banner Ad Sample  
    < !-- -  
        This is an ad unit ID for a banner test ad. Replace with your own banner ad unit id.  
    -->  
    < string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111< /string>  
  < !--  < string name="title_activity_banner">BannerActivity< /string>-->  
< /resources>  
< resources>  
    < string name="app_name">Kotlin Banner Ads< /string>  
    < string name="banner_ad_sample">Banner Ad Sample< /string>  
    < !-- -  
        This is an ad unit ID for a banner test ad. Replace with your own banner ad unit id.  
    -->  
    < string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111< /string>  
  < !--  < string name="title_activity_banner">BannerActivity< /string>-->  
< /resources>

MainActivity.kt


Add the following code in MainActivity.kt class. To load the ad on the UI, create the instance of AdRequest and load the ad in AdView by calling AdView.loadAd(AdRequest).
Override the AdView listeners onAdFailedToLoad(), onAdLoaded(), onAdOpened(), onAdClicked(), onAdClosed(), etc.


package example.javatpoint.com.kotlinbannerads  
  
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import android.view.View  
import android.widget.Toast  
import com.google.android.gms.ads.AdListener  
import com.google.android.gms.ads.AdRequest  
import com.google.android.gms.ads.AdView  
  
class MainActivity : AppCompatActivity() {  
    lateinit var adView : AdView  
  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
        // Load an ad into the AdMob banner view.  
        adView = findViewById< View>(R.id.adView) as AdView  
        val adRequest = AdRequest.Builder().build()  
        adView.loadAd(adRequest)  
  
        adView.adListener = object : AdListener(){  
            override fun onAdFailedToLoad(p0: Int) {  
                super.onAdFailedToLoad(p0)  
                val toastMessage: String = "ad fail to load"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdLoaded() {  
                super.onAdLoaded()  
                val toastMessage: String = "ad loaded"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdOpened() {  
                super.onAdOpened()  
                val toastMessage: String = "ad is open"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdClicked() {  
                super.onAdClicked()  
                val toastMessage: String = "ad is clicked"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
  
            override fun onAdClosed() {  
                super.onAdClosed()  
                val toastMessage: String = "ad is closed"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdImpression() {  
                super.onAdImpression()  
                val toastMessage: String = "ad impression"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdLeftApplication() {  
                super.onAdLeftApplication()  
                val toastMessage: String = "ad left application"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
        }  
    }  
  
    override fun onPause() {  
        if (adView!=null) {  
            adView.pause();  
        }  
        super.onPause()  
    }  
  
    override fun onResume() {  
        super.onResume()  
        if (adView != null) {  
            adView.resume();  
        }  
    }  
  
    override fun onDestroy() {  
        if (adView != null) {  
            adView.destroy();  
        }  
        super.onDestroy();  
    }  
}  
package example.javatpoint.com.kotlinbannerads  
  
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import android.view.View  
import android.widget.Toast  
import com.google.android.gms.ads.AdListener  
import com.google.android.gms.ads.AdRequest  
import com.google.android.gms.ads.AdView  
  
class MainActivity : AppCompatActivity() {  
    lateinit var adView : AdView  
  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
        // Load an ad into the AdMob banner view.  
        adView = findViewById< View>(R.id.adView) as AdView  
        val adRequest = AdRequest.Builder().build()  
        adView.loadAd(adRequest)  
  
        adView.adListener = object : AdListener(){  
            override fun onAdFailedToLoad(p0: Int) {  
                super.onAdFailedToLoad(p0)  
                val toastMessage: String = "ad fail to load"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdLoaded() {  
                super.onAdLoaded()  
                val toastMessage: String = "ad loaded"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdOpened() {  
                super.onAdOpened()  
                val toastMessage: String = "ad is open"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdClicked() {  
                super.onAdClicked()  
                val toastMessage: String = "ad is clicked"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
  
            override fun onAdClosed() {  
                super.onAdClosed()  
                val toastMessage: String = "ad is closed"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdImpression() {  
                super.onAdImpression()  
                val toastMessage: String = "ad impression"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
            override fun onAdLeftApplication() {  
                super.onAdLeftApplication()  
                val toastMessage: String = "ad left application"  
                Toast.makeText(applicationContext, toastMessage.toString(), Toast.LENGTH_LONG).show()  
            }  
        }  
    }  
  
    override fun onPause() {  
        if (adView!=null) {  
            adView.pause();  
        }  
        super.onPause()  
    }  
  
    override fun onResume() {  
        super.onResume()  
        if (adView != null) {  
            adView.resume();  
        }  
    }  
  
    override fun onDestroy() {  
        if (adView != null) {  
            adView.destroy();  
        }  
        super.onDestroy();  
    }  
} 

< meta-data  
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"  
            android:value="true"/>  
< meta-data  
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"  
            android:value="true"/>   

in AndroidManifest file.


AndroidManifest.xml


Add the following code in AndroidManifest.xml file


< ?xml version="1.0" encoding="utf-8"?>  
< manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="example.javatpoint.com.kotlinbannerads">  
  
    < !-- Include required permissions for Google Mobile Ads to run. -->  
    < uses-permission android:name="android.permission.INTERNET" />  
    < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  
    < application  
        android:allowBackup="true"  
        android:icon="@mipmap/ic_launcher"  
        android:label="@string/app_name"  
        android:roundIcon="@mipmap/ic_launcher_round"  
        android:supportsRtl="true"  
        android:theme="@style/AppTheme">  
        < activity android:name=".MainActivity">  
            < intent-filter>  
                < action android:name="android.intent.action.MAIN" />  
  
                < category android:name="android.intent.category.LAUNCHER" />  
            < /intent-filter>  
        < /activity>  
        < !-- This meta-data tag is required to use Google Play Services. -->  
        < meta-data  
            android:name="com.google.android.gms.version"  
            android:value="@integer/google_play_services_version" />  
        < meta-data  
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"  
            android:value="true"/>  
        < activity  
            android:name="com.google.android.gms.ads.AdActivity"  
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"  
            android:theme="@android:style/Theme.Translucent" />  
    < /application>  
  
< /manifest>  
< ?xml version="1.0" encoding="utf-8"?>  
< manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="example.javatpoint.com.kotlinbannerads">  
  
    < !-- Include required permissions for Google Mobile Ads to run. -->  
    < uses-permission android:name="android.permission.INTERNET" />  
    < uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  
    < application  
        android:allowBackup="true"  
        android:icon="@mipmap/ic_launcher"  
        android:label="@string/app_name"  
        android:roundIcon="@mipmap/ic_launcher_round"  
        android:supportsRtl="true"  
        android:theme="@style/AppTheme">  
        < activity android:name=".MainActivity">  
            < intent-filter>  
                < action android:name="android.intent.action.MAIN" />  
  
                < category android:name="android.intent.category.LAUNCHER" />  
            < /intent-filter>  
        < /activity>  
        < !-- This meta-data tag is required to use Google Play Services. -->  
        < meta-data  
            android:name="com.google.android.gms.version"  
            android:value="@integer/google_play_services_version" />  
        < meta-data  
            android:name="com.google.android.gms.ads.AD_MANAGER_APP"  
            android:value="true"/>  
        < activity  
            android:name="com.google.android.gms.ads.AdActivity"  
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"  
            android:theme="@android:style/Theme.Translucent" />  
    < /application>  
  
< /manifest>  

Output:


Hi I am Pluto.