Follow treslines by email clicking Here!

Tuesday, May 13, 2014

How to add advertisement (monetize) to your mobile app in android

Hi there!

today i'm gonna show, how to embed advertisements (AdMob) in your android application. I found the tutorials on the internet not so intuitive and that's the reason i'm sharing this with you. It is very streight forward if you know how... ;)

In my case, the end product was a very simple application with a single banner advertisement on the middle of the screen. It is up to you where to place it. My end result is a street narrator. It speaks to you loud the name of the streets you drive thru. This way you can concentrate on transit, if you are looking for a specific address. It is awesome if you are a courier or driving in a foreign country. It speaks 6 diffent languages. It also alerts you if you are driving to fast. Specially in countries with bad signalization it is wonderful. Search for Road Narrator on Google Play Store if you want to test it. Here is a screen shot of it:

 

Important: Bevor you start with this post, make sure you have a valid AdMob account and valid AdSense and AdWord Accounts on Google. Otherwise at the end of this post you'll not be able to test it. (without it, you'll not be able to generate your AdMod advertisement key)

Step 1:

if you don't have it already, please make sure you have downloaded the google play services like shown in the picture bellow:

Step 2:

Import the downloaded google play service lib as shown. Locate your android sdk and look for the extra folder. In there it will be the  google_play_service_lib.



Step 3:

Right click on your project > properties > Android > select the new available, imported google_play_services_lib by pressing add as shown:


Step 4:

In your project manifest make sure you have defined the followed entries:

Step 5:

In your activity_main.xml layout define (anywhere you want and need) a linearlayout like this:

Step 6:

On the top of your MainActivity define those private variables. Please NOTE, that the MY_AD_UNIT_ID is a ID you'll get as soon as you have your AdMob account. The id bellow is a fake one. (you don't want my ID right? ;)

Step 7:

At the end of your onCreate() method of MainActivity.java call the method createAdView() like definition bellow: (create it in your own) 



Then make sure you have the following entries in the methods onPause, onDestroy and onResume:

That's all! You should now be able to run it and if you are connected with the internet, you will see your first advertisement banner on the screen in the place you've defined.

Hope you like it. ;)