AdMob is Google’s advertising platform specifically designed for mobile applications. AdMob allows you to monetize your apps by serving ads selected according to criteria you define. So if you’ve developed an Android app and now you want to make money with your app, in this article I show you how to do it, how to add different ad formats to your apps.
To get started, make sure you have integrated Firebase into your project. If you haven’t already done so, here’s how to do it.
Set up your application in your AdMob account :
- To get started you need to create an AdMob account or sign in if you already have one.
- Login to AdMob and open your dashboard. Now, go to Applications> Add an Application> Fill out the information required by AdMob> Click Add, which will generate an AdMob ID for your application.
- To link your newly added application to Firebase. Select the application, access the application settings and under Application Information you will see an option to link the application to Firebase.
- Add your AdMob Application ID to your AndroidManifest.xml file by adding the <meta-data>tag.
<meta-dataandroid:name="com.google.android.gms.ads.APPLICATION_ID"android:value="ADMOB_APP_ID"/>
implementation 'com.google.android.gms:play-services-ads:19.1.0'
class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)MobileAds.initialize(this, "YOUR_ADMOB_APP_ID")}}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:ads="http://schemas.android.com/apk/res-auto"tools:context=".MainActivity"android:orientation="vertical"><com.google.android.gms.ads.AdViewandroid:id="@+id/adView"android:layout_height="wrap_content"android:layout_width="match_parent"ads:adSize = "BANNER"ads:adUnitId = "ca-app-pub-3940256099942544~3347511713"></com.google.android.gms.ads.AdView></LinearLayout>
import android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport com.google.android.gms.ads.AdRequestimport com.google.android.gms.ads.AdViewimport com.google.android.gms.ads.MobileAdsclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)MobileAds.initialize(this) {}val mAdView = findViewById<AdView> (R.id.adView)val adRequest = AdRequest.Builder (). build ()mAdView.loadAd (adRequest)}}
|
2. Creation of interstitial ad
class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)MobileAds.initialize(this) {}val mInterstitialAd = InterstitialAd(this)mInterstitialAd.adUnitId = "ca-app-pub-3940256099942544/1033173712"}}
mInterstitialAd.loadAd(AdRequest.Builder().build())
clickButton.setOnClickListener {if (mInterstitialAd.isLoaded) {mInterstitialAd.show()} else {Log.d("TAG", "The ads wasn't loaded yet.")}}
App Id = ca-app-pub-3940256099942544~3347511713Ads Id =ca-app-pub-3940256099942544/1033173712