Admob android ads integration using Android Studio

Vamsi Tallapudi Android 3 Comments

In this tutorial we are going to discuss about admob android ads integration into your app using Android Studio. Admob is the most reliable solution for advertisements offered by google for android and ios Devices. Many use admob ads for their high fillrate and high eCPM(Effective Cost per Mille).

In this article we are going to integrate ads into a simple demo android project. These steps can be applied to any android project in which you want to integrate your ads.

Creating a simple Android Project:

  1. First we create a project by going into File > New Project and we simply name it as AdmobDemo. Hit Next.Admob Android Ads integration - Creating New Project
  2. Next we Choose minimum SDK and hit Next. For more assistance on Creating Android Project, refer to this article.
  3. We select Blank Activity and hit Next. We leave the Activity Name, Layout Name and Title Name as default values and hit Finish.

Now we have created a basic android project which is displaying default text – “Hello World!” . Now let us discuss about Admob android ads integration.

Admob android ads integration into Android Project:

  1. First of all make sure you installed Google Repository. If not, go to SDK Manager > Extras and install Google Play Services and Google Repositories. The package contains all the classes you need to display your ads such as Google Mobile Ads SDK.
  2. Now, Open your project’s Gradle Scripts > build.gradle(Module: App) and add this piece of code to your dependencies. This compiles and adds play services to your project which you use them to display your ads.
    Here I have used the version 7.5.0 at the time of writing/modifying this article. To make sure that you always use the latest version of Google Play Services, update Google Play Services in SDK Manager and Go to File> Project Structure. Then on the Left, select App displayed under Modules. Select dependencies tab and click on “+” icon.  Then click on Library Dependencies and select play-services and Hit Ok.
  3. Now the android studio adds the dependencies for your project. Make sure that you added this piece of code to your Module: App’s build.gradle  but NOT to your project’s build.gradle. Hit Sync Now and Save the file. Now your app’s build.gradle’s code will look similar to this:
  4. Save file and Open your Manifest file by going to Apps > manifests > AndroidManifest.xml. Now we need Internet permission for Admob android ads integration. This permission is obtained by adding the following lines to your Manifest file.
  5. Now, to use play services to display our ads , we need to add the following code as meta-data tag:
    We add them in between our application element.

    We also need an activity to display admob android banners and interstitials when an ad is clicked. So we declare that activity in our manifest file by adding the following code:

  6. After adding all the above lines of code the manifest looks like this:

    Now save AndroidManifest.xml and open the layout activity_main.xml which is created by default. Now open text tab located below and add the following code to display the ads on the bottom of your layout.

  7. You also need to make another change to your layout. You need to add the following parameter to your root Relative Layout:

    Now the activity_main layout’s code will look like this:

  8. Now hit save. You will get error as banner_ad_unit_id is not yet created. So we will create that in our next step.
  9. Go to app > res > values >strings.xml and open it. Add your publisher id in the string. The below line contains demo publisher id by google. Replace it with your own publisher id to show your ads.

    The publisher id for your app is created when you sign in into Admob and clicked Monetise New App. Your publisher id is generally of the form: ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxx.

  10. Now Hit save and the error in activity_main.xml file will disappear. If not, check for typos.
  11. Now open the activity in which you want to display your ads. In this case it is MainActivity.java. Add the following lines of code in your OnCreate() method after setting your content view to the layout which contains the play services code ( Here it is set to activity_main.xml):
    Import the required classes (AdView, AdRequest) by clicking Alt + Enter and Hit save. Now you can display your ads by admob within your app as seen below.Admob android ads - Emulator displaying Android banner ads

    Note: Make sure that you never click your own ads, even for testing as it may lead to permanent ban on your admob / adsense account. For testing purposes use the google’s demo publisher id which we used in the above example.

 

Related Posts