There has been a lot written about how to monetize your mobile app, and you can read some about it in one of our future posts. For this post we are going to explore adding Ads to your app to drive monetization.
We will focus on Android for this post, to start with we will need to download the Google Play services
The AdMob API currently support banner and interstitial ads, and one of the first decisions you will need to make is determine which of the ad formats work best for your application. You could also use both together if you feel that works for your application.
The key to using ads is to make them as unobtrusive as possible, ad supported applications are becoming more of the norm these days as most consumers drift towards free services.
Interstitial ads are rendered full screen taking over the whole display area of the application, once visible they give the user the opportunity to dismiss the add or to redirect to the advertised product page. These types of ads are generally better suited for games and applications that transition between different pages on a regular basis.
Banner ads, only occupy a designated portion of the screen, they therefore stay visible for much longer periods, while not interfering with the users experience. These types of ads are well suited for single page applications like maps or document readers.
Before we begin I am going to assume that you have already gone over to the AdMob site and sign up for an account. If not now is a good time to do so, you will need your unique ad unit id’s to complete this tutorial.
We will start by adding the Google Play Services to our modules dependencies list in Android Studio.
1) Right click on your app module and select Open Module Settings
2) Select the Dependencies Tab and Click the + button and select 1 Library Dependencies
3) Find the com.google.android.gms:play-services-ads and click Ok and click Ok again to close the Module Settings Page
With our module dependencies in place we can not start making the necessary changes to the code.
Adding a Interstitial Ad to your application
Incorporating interstitial ads in your application requires the following steps:
1) Add an interstitial variable in the main application page by adding the following code to our MainActivity.java file.
2) Instantiate our InterstitialAd class and set the Ad Unit ID by adding the code below
3) Request and load the ad, by adding the following code.
4) Now setup a listener for to receive the onAdLoaded event, like we did below
5) Lastly we want to add a new method to the MainView class to show our Ad
If you build your application now and run it on your emulator you should see the below screen after a few seconds when the application starts.
Congratulations you have successfully added an interstitial Ad to your application, you can now customize how often this ad displays by going to your AdMob account.
Adding a Banner Ad to your application
Incorporating a banner ad into your application requires the following steps:
1) Create a new layout item for your banner, I chose to add this above the list in the chat_list.xml layout file.
2) In the ChatList.java within the OnCreateView method, get a reference to the AdView component that we previously added to the chat_list.xml layout file.
3) Create a new AdRequest object and load the new request into the AdView component
If you build your application now and run it on your emulator, and navigate to the chat list page you will notice the banner ad on top of the list as seen in the below capture.
Once again congratulations if you have been following along, you have now successfully added a banner ad to your mobile application.