How To Insert AdMob Banner Ad In Your Android App

In this article, I will show you how to insert Banner Ad code in your Android Application. Before that let me give you a brief idea of what is Banner Ad.

Banner Ad is one of the Ad types of AdMob which helps you in generating revenue by displaying Ads on your App. To learn more about AdMob and Banner Ad click on the link provided below.

What is AdMob and how to generate Ad unit ID?

There are total four types of AdMob Ads through which you can earn money. You can click on the link below to learn more about other types of AdMob Ads.

  1.  Banner Ad
  2.  Native Ad
  3.  Rewarded ad
  4.  Interstitial Ad

Now, let’s get started with how to place the code of Banner Ad in your Android Application. I have assumed that you are using Android Studio for developing your app and your project is opened in it. Follow the below steps and copy paste the code into your project carefully.

⟩⟩  First of all, you need to add below line of code in your build.gradle dependencies file.

compile 'com.google.android.gms:play-services-ads:8.4.0'

after adding the above code you will get sync now option at the top right corner of the page. Click on that and wait for a few minutes to complete the process, once the gradle building is finished you are ready to go with the next steps.

⟩⟩ Then open the Manifest file and add Internet permission to it.

<uses-permission android:name="android.permission.INTERNET" />

⟩⟩  In project structure, navigate to activity_main.xml and paste the following code in your layout.

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

and in the same file i.e activity_main.xml add below lines of code in the header part

xmlns:ads="http://schemas.android.com/apk/res-auto"

⟩⟩  Now open MainActivity.java and add below lines of code in onCreate method

AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

⟩⟩  Now open string.xml file in values folder and paste the below line of code.

<string name="ad_unit_id">ca-app-pub-39402560999xxxxx/21772xxxxx</string>

Banner AdMob ID used here is a sample ID. In order to get the revenue, you need to replace this test ID with your own Banner Ad ID.

If you don’t know how to get your own Banner Ad Id then click here on how to get Banner Ad Unit ID.

Note: Don’t use your real Ad Unit ID while testing and never click on your own Ads it is against AdMob Policy.

That’s it, friends, this is the correct and simple way to insert Banner Ad in your Application. Just follow the above steps and copy paste the code carefully at the correct location mentioned above.

If you are a beginner you will love to learn Android Application development from scratch.

If you found this article helpful please do comment below and share the article to support me. If you have any suggestion please feel free to comment below.

Hostinger Special offer

hostinger review

Get Now

We use cookies for essential website functions and to better understand how you use our site, so we can create the best possible experience for you ❤️

elegantespace logo png

How to insert AdMob Banner Ad in your Android App (Write Your Opinion)