Android How to Integrate Google AdMob in your App

AdMob is a multi platform mobile ad network that allows you to monetize your android app. By integrating AdMob you can start earning right away. It is very useful particularly when you are publishing a free app and want to earn some money from it.

Integrating AdMob is such an easy task that it takes not more than 5mins. In this article we’ll build a simple app with two screen to show the different types of ads that AdMob supports.

1. Type of AdMob Ads

AdMob currently offers below types of ad units. You can choose the ad format depending on your app criteria.

> Banner AdBanner Ads occupies only portion of the screen depending on the ad size that is created. It comes in multiple sizes Standard, Medium, Large, Full-Size, Leaderboard and Smart Banner. Smart banners are very useful when you target multiple device sizes and fit the same ad depending on the screen size.

> Interstitial AdInterstitial ads occupies full screen of the app. Basically they will shown on a timely basis, between screen transition or when the user is done with a task. Usually we can see these ads in games displaying Ad when a level is completed.

> Rewarded Video AdRewarded Video Ads are fullscreen video ads which offers some reward points if the user watches the ad video. These ads are very useful to offer some reward points / coins in video games.

> Native AdNative Ads offers flexibility to configure the ad appearance like color, text color and buttons to make them appear as native component of the app.

2. Creating Ad Units

2. Create a new App by giving the package name of the app you want to integrate AdMob. Once the App is created, you can find the APP ID on the dashboard which looks like ca-app-pub-XXXXXXXXX~XXXXXXXXX.

3. Select the newly created App and click on ADD AD UNIT button to create a new ad unit.

4. Select the ad format and give the ad unit a name.

5. Once the ad unit is created, you can notice the Ad unit ID on the dashboard. An example of ad unit id look like ca-app-pub-066XXXXXXX/XXXXXXXXXXX

Create as many ad units required for your app.

3. Creating New Project

1. Create a new project in Android Studio from File ⇒ New Project. When it prompts you to select the default activity, select Empty Activity and proceed.

2. Open build.gradle and add play services dependency as AdMob requires it.

4. Create a class named MyApplication.java and extend the class from Application. In this application class we have to globally initialize the AdMob App Id. Here we use MobileAds.initialize() method to initialize the AdMob.

(Note: App ID is different from Ad Unit ID. Place the App ID carefully)

3.1 Adding Banner Ad

Banner ads occupies only a portion of the screen. I am adding a banner ad in my main activity aligning to bottom of the screen. In order to add the banner ad, you need to add com.google.android.gms.ads.AdView element to your xml layout.

Now if you run the app, you should see a banner ad at the bottom of your screen.

3.2 Adding Interstitial Ad (Fullscreen Ad)

Interstitial ads occupies full screen of the app. Adding interstitial ad doesn’t require an AdView element to be added in the xml layout. Rather we load the ad programatically from the activity. Normally these ads will be populated when user is moving between activities or moving to next level when playing a game.

We’ll test this ad by creating a second activity and popup the full screen ad when the second activity is launched.

8. Create an activity named SecondActivity.java by right clicking on package New ⇒ Activity ⇒ Empty Activity.

Now if you run the app, you can see the interstitial ad when the second activity is launched.

3.3 Adding Rewarded Video Ad

9. Create another activity named RewardedVideoAdActivity.java and add the below code. This is same as Interstitial but there will be a callback method onRewarded() called when there is a reward after watching the video ad completely.

3.4 Enabling Test Ads

As per AdMob Policies you are not allowed to click on your own live ads. In order to protect your AdMob account from getting suspended, use test ads during development as you might click the ads accidentally.

When you run the project, if you monitor the LogCat, you can find a similar line Use AdRequest.Builder.addTestDevice(“C04B1BFFB0774708339BC273F8A43708”) to get test ads on this device. Copy the device id and add it to AdRequest as shown below. Note that this ID varies from device to device, By doing this, the test ads will be loaded instead of live ads.

In production you need to make sure that you removed addTestDevice() methods in order to render the live ads and start monetization.

4. Known Issues

While running the app, the Ads might not display and the below errors can be seen in LogCat.

> There was a problem getting an ad response. ErrorCode: 1> Failed to load ad: 1
If you happen find the above errors in your LogCat, there is no need worry. The newly created Ad Units takes few hours to display the actual ad. Until then you will see these errors. The best way to resolve this problem is to wait for few hours and test the app again.

Hi there! I am Founder at androidhive and programming enthusiast. My skills includes Android, iOS, PHP, Ruby on Rails and lot more. If you have any idea that you would want me to develop? Let’s talk: ravi@androidhive.info

Place these ads in your apps. You will get money when somebody clicks on the ad.

Thirumaran

thank u so much………

bhavik gandhi

Hello, I want to Implement Video Ads
So please any other that display video ads and alternative of admob

Eyal Engel

Can I put an ad of my own and get money for it? or do I need to close a deal with a real company who wants to advertise on my app?
e.g. Can I put an ad that says: “click me”, and get money if someone clicks it (although it will do nothing special…?

Hi plss dont mind if i ask you a questn Ravi Tamanda do you use a commenting 3rd party or this comment section is made by your own??? Plss let me know if your using a 3rd party..then i will also use it..thnxx in advance..

Hello Ravi, From many times i am folowing your tutorials and its very helpful, one problem i am suffering from 2-3 days is that OutOfMemory error in loading images. i have image automatic slider in my app. so when i start running on mobile , it throws error of outofmemoryerror in logcat . please help me to solve.

I did not know how to set background of layout or load image from Bitmap drawable so i directly load in tag.

Hi Ravi adMob is great, No doubt. but it is not showing any impression on slideMe market devices and not even Analytics are working there. Could you suggest me something for this context.

Viet Nam

HI Thanks
very useful
Sorry please for me ask
I viewed all your post on android but not view post toturial Service of android.You can write post service Android ?
If can i very grateful you because service i see it very important in some app good.Thanks

Deep Shikha

Hi Ravi,
Hope you are doing well. I have followed you on numerous topics for learning android app development. Please advise if you also take classes or guide.

I enjoyed your tutorials. I hope you do a tutorial on integrating Native ads admob or facebook into RecyclerView .

Thanks,

Regards

Sami

Hi Ravi, Im totally new to this stuff so sorry for noob questions.

I am using Navigation-Drawer in my application(i have followed your material desing tutorials). So I have created single activity with navigation_drawer layout. I am using fragments to change main content area data whenever user chooses a menu option from navigation drawer menu.Now my problem is that I want to show admob ad on every screen and when opening the navigation drawer the ad will be under the navigation menu.

My question is: is there any conflict or contrast with the AdMob Policies ?

Hello ravi, I have a small issue.I don’t think how logical it is. What if I wished to add only desired app from playstore. For example I just want to display facebook and twitter add randomly.How can I do that if it is possible to do?

Manish Butola

Great Tutorial !

Rahul

Hello Ravi bhai, great tutorial. Can you please tell me how do you created that video.Please.

i face a problem console error :- Failed to load ad: 0
how to solve this problem.

Ahmed Buchery

I’m facing the same issue, my app is play store but with no ads.

While in development I’ve added the .addTestDevices but on production I removed it and no one that downloaded the app sees ads.

I have create a new ad-unit with no luck

FaisalHyder

As salam o alaikum, issue is that you guys might be taking app id from app-management page on admob account, don’t take it from there take app-pub id from Monetize tab and under all apps check the ad unit you made use this id in app. hope this helps.!!

Syed Farrukh

Hi Ravi …
Thanks for the tutorial everything is working fine…. can you please guide me how to integrate Video ads in android studio and one question more how to generate video Ad id in admob …. Thank you in advance dear

hello every body,
it’s been 24 hours i have this problem please any one could help
the error is : Error inflating class com.google.ads.AdView

Rishab Surana

I wonder you are working in eclipse
if so then i recommend you to switch to Android Studio coz i also had this problem and couldn’t find any solution for this but if you are working in Studio then there should’t be any problem if you have included the google play services in the app
But if u get any solution to this don’t forget to share it
Hope it helps

Can someone create this app to show ad for me. I will provide all the details necessary.

Chris Harris

where is your app?

Sachin Vaghela

Hi Ravi,
Your Code is very helpful to us. but my question is i saw some different steps to Integration Google Admob in application here the link is : https://firebase.google.com/docs/admob/android/quick-start
I implement your code and its working very well but i want to know which is the right way to integration?

hi when I show interstitial ad the firstly click button it is open add and open new activity, but second click inactive… it is not working. how can refresh button when start interstitial and open that button for next activity

Hirpara Vivek

how to set adblock in my app

Swati

Hi Ravi, your tutorials are great and very helpful. In the above banner ad code there is no initialization without which it was not showing the banner.

Sandeep

Sir in 3.1 Adding Banner ad what is xml

Sandeep

What is xml layout in 3.1 where to ad these codes

Saidur Rahman

Hi Ravi,

I used exactly your code for Interstitial ads. It was all working fine for long time. But today I got email like this

hello sir….I have done all this things…but these ads are only being shown when I am testing it on android emulator (Android Studio)…But when I am testing it on an actual device (like my phone),I can’t find these ads….So please tell me what should I do????

name

it takes a few hours

Amit Raj

Hello Ravi,
Is it necessary to pay money in admob account because it’s automatically turns into beta version.And after creating banner and interstitial ad i am unable to display ad on my device.I have downloaded your apk and that is also not working.Please reply….

No, this comes under Google Adsense policy violation. Google will identify you easily and disable the AdMob / Adsense account. All the employees will be in same network/location, so finding the fraudulent clicks is very easy.

Shivam Bhardwaj

Sir we are not going to click any ad, i was just thinking for the money from ad impression not by clicking it. I knew it very well that clicking ads by own is offensive but I just want our office app to display ads and in month end we will get some little money for impressions.

No problem at all. Test device id is to display the test ads while developing the app, so that you don’t click on real ads and get your account banned. It’s always suggested use test device id to avoid admob account deactivation.

Here we have to Replace ” InterstitialAdActivity ” in MainActivity by ” SecondActivity “. After that error will get cleared.
Very Useful Video for Banner Ad and Interstitial Ad but i m not getting Reward VideoAd. So if i miss anything then please give me Guidance.

The other one Error i have faced is about Banner size in java file(Comment it) and Banner adUnitId(Comment it). So after that it will work perfectly.

Hai Ravi.. How does Admob know that I have clicked on my own ad? And is removing addTestDevice() methods the only thing required to make the app live for production? Can I test live ads using other devices apart from mine?

They track the clicks by IP address and other device coming from device. Removing addTestDevice() makes the ads live and you should see the live ads getting displayed. You don’t have to test the live Ads, they works well without any problem.

My suggestion is, don’t play with live ads. I myself lost lot of Adsense / AdMob account because of this article particularly.

Chobela Kakumbi

Ok Ravi, thanks.

Najaf Ali

Hi Ravi is it important to add ad mob app id in our app, if dint include this in our app, and just banner and interstitial ids implemented, then it would be correct ?? what is benefit to include ad mob app id

DaVid Silence Kawlni

Could not resolve com.google.android.gms:play-services-ads:11.8.0

Im still stuck here. What to do? Please help…

Gowtham Nimmagadda

Hi Ravi. Do we really need the adListener in our app, if we don’t perform action on Ad clicked / opened ?