Adding Ads to Android App using Google Admob

Making apps are always hard work. This includes days/months of testing and feature iterations. Implementing ads can be one solution where your work finally gets paid off. Google's Admob is an efficient SDK when it comes to app monetisation. Let us see how to implement Admob ads into our android application.

1. Setting up ad units in the admob dashboard.
2. Implementing the created ad units in our app.

1. Setting up ad units in the admob dashboard.

Step 1 : head over to https://apps.admob.com/ and sign in. After proper login, you would see an option named Apps in the left sidebar, click it. Click 'add app' option on the popup that follows.

Step 2 : After setting up the app as instructed in previous step, select the created app from the above menu and click on Ad Units from the left sidebar menu as shown below

Now click on "ADD AD UNIT" from the next screen. This will take you to the screen where you can select the ad type : banner, interstetial or rewarded ad type. In this tutorial we will be covering the banner ad type. Banner ads are rectangular strip ads that looks like the following

Select banner type and complete the ad unit creation by entering the details. You will reach this screen finally

In the above image, the id given in the first step with a "~" somewhere in between the id is called the app ID and the id given in second step with a "/" somewhere in between the id is the ad unit id. Now make note of these two ids. We will require them while we setup the code.

Now we need to add the app id that we have obtained from our admob dashboard previously(one with the ~ in between) to our AndroidManifest.xml like shown below.Here I'm adding test app id provided by admob(you may use this for testing but change it to your app id from dashboard while going live)

In the above declared adview, you need to place the ad unit id from the admob dashboard when going live or you can use the above test value for testing (the one with "/" in between). We have created this earlier along with app id

Note : Never use your production values of ad unit id, app id for testing purposes(doing so may get your account deactivated, although doing so few times for checking is not a problem). It is recommended that you use these test values : ca-app-pub-3940256099942544~3347511713 (test app id), ca-app-pub-3940256099942544/6300978111(test ad unit id). You may change this to the production values(the one you created in admob dashboard) while going live

Now go to your MainActivity.java file and then initialise and make an ad request like this following

Now run your app and you could see test ads being displayed like below

Now change the test credentials to the ids mentioned in admob dashboard and see if live ads are appearing. Live ads will be similar to the test ads but it wont be having a test ad label like the one above. Don't worry if the ads not appearing when you added your live credentials, admob might take a few hours after ad unit to setup the ads for you. It will start showing ads from then on.