Admob Banner Ads in Android


What are Admob Banner Ads in Android?

Admob is a Google mediation platform. We can use this to display advertisements within our android application. Along with that we can earn money by displaying the ads within our application. There are different types of ads which we can display within our application. In this article we will take a look on How to display Admob Banner ads within our android application.

Implementation of Admob Banner Ads in Android

We will be creating a simple application in which we will be simply displaying a text view and an Admob AdView. Using this adview we will be displaying our banner ad. We will be following a step by step guide to implement a Motion Layout in our Android application using Kotlin.

Step 1 : Creating a new project in Android Studio

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 2 : Adding dependency in build.gradle file to use this library

Navigate to Gradle Scripts>build.gradle file and add the below dependency in dependencies section.

implementation 'com.google.android.gms:play-services-ads:21.5.0'

After adding the above dependency you will get to see the Sync Now option in the top right corner of your IDE. Simply click on it to sync your project and install this dependency within your project.

Step 3 : Adding Admob Application ID in our AndroidManifest.xml file

Navigate to app>AndroidManifest.xml file and add inside the application tag. Add below line.

<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713" />

Explanation − In the above code we are specifying the name as application id and passing the value for it as Application ID. Currently we are specifying Admob test app id for the purpose of testing. Later on we can change this by adding the original id provided by Admob.

Step 4 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity"> <!-- on below line creating a text view for heading of application--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_margin="10dp" android:text="Admob Banner Ads in Android" android:textAlignment="center" android:textColor="@color/black" android:textSize="25sp" android:textStyle="bold" /> <!-- on below lin creating an AdView ans specifying adsize as banner--> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" 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="ca-app-pub-3940256099942544/6300978111" /> </RelativeLayout>

Explanation − In the above code we are creating our root layout as Relative Layout. Inside this layout we are creating a simple text view to display the heading of our application.

After that we are creating an AdView inside this adView we are specifying height and width as wrap_content. Along with that we are specifying id for it. After that we are aligning this adView to the bottom of the screen. Then we are aligning this adview to the center horizontal of the screen. After that we are setting the ad size as Banner ad and lastly we are adding adUnit ID to display the Ad. In the above code we are using the test ad unit id provided by Admob for the purpose of testing. We can change it with the ad unit id provided by Admob.

Step 5 : Working with MainActivity.java file

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>java>your app’s package name.MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterViewFlipper; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.initialization.InitializationStatus; import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line initialing mobile ads. MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { // on below line displaying a log that admob ads has been initialized. Log.i("Admob", "Admob Initialized."); } }); // on below line creating and initializing variable for adView. AdView adView = findViewById(R.id.adView); // on below line creating and initializing variable for adRequest AdRequest adRequest = new AdRequest.Builder().build(); // on below line loading request inside our adview. adView.loadAd(adRequest); } }

Explanation − In the above code inside our on Create method we are firstly initializing our Mobile Ads and inside on Initialized complete listener method we are simply displaying a log message. After that we are creating and initializing variables for adView with the id which we have given inside our xml file. After that we are creating an adrequest to request the ad and lastly setting that ad request to load our ad inside our adView.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Conclusion

In the above tutorial we what are Admob Banner Ads in android and how we can implement them within our android application and earn money from it.

Updated on: 14-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements