AdMob Banner Ads of andorid

A banner ad is a rectangular picture or text ad that appears in the layout of an application. The simplest to use if you're new to mobile advertising are banner ads. This post explains how to include AdMob banner ads into an Android application.

In this post, we will look at the process of adding a admob to an application by creating an app  of Mobile Application

Exmaple 

                                              


Dependencies of admob

  implementation(group = "com.google.android.gms", name = "play-services-ads", version = "19.3.0")
Step-by-Step Implementation

Step 1: Create a New Project in Android Studio (File >new  project). 



Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

Implement the same in​voke the following code inside activity_main.xml file. 

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<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"
app:adSize="BANNER"
app:adUnitId="@string/admob_banner_id"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 


Step 3: Create an MainActivity.kt file in your package folder .

Implement the same invoke the fol​lowing code inside MainActivity.kt file.

 import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds


class MainActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
lateinit var adView: AdView
@SuppressLint("VisibleForTests")
lateinit var adRequest: AdRequest
@SuppressLint("VisibleForTests")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//initialize Mobileads
MobileAds.initialize(this)
//create object
adView = findViewById(R.id.adView)
//adrequest builder
adRequest = AdRequest.Builder().build()
//load ads
adView.loadAd(adRequest)
}
}
Step 4 : Output of above example

                                               

Happy coding!


365Bloggy July 12, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive