:-: Google AdMob in AndEngine Tutorial :-:

  ... tutorials on how to use AndEngine.

:-: Google AdMob in AndEngine Tutorial :-:

Postby UmzGames » Mon Feb 04, 2013 11:00 pm

This is a tutorial for using Google adMobs in your AndEngine projects on Eclipse without XML.
Firstly, there are other posts on this :arrow:, however, I did not find one that worked clearly and completely for me so I'm posting here (Maybe I just found them difficult to understand :oops:)

You must sign up to adMobs, it is free and only takes a little while. You will need your address and some way to recieve payment: I used direct transfer to bank; not certain of the other options. :money:
* I suggest you design your apps with ads in mind so they have a good fit in your app, and also look at the available sizes of ads. :check:

Get the SDK and follow the instructions here to add it to your project.
* Download the SDK, add it to your project and change the manifest :check:
* MAKE SURE you update the android:configChanges - will cause errors without these :exclamation:

Get your Publisher ID from the adMob website: click here and follow the simple steps.

Now in your Main activity use the code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public BaseGameActivity { // <- I know
  3.   AdView adView;
  5.   /** MY Class Comment */
  6.   @Override protected void onSetContentView() {
  7.         // CREATING the parent FrameLayout //
  8.         final FrameLayout frameLayout = new FrameLayout(this);
  10.         // CREATING the layout parameters, fill the screen //
  11.         final FrameLayout.LayoutParams frameLayoutLayoutParams =
  12.                 new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
  13.                                              FrameLayout.LayoutParams.MATCH_PARENT);
  15.         // CREATING a Smart Banner View //
  16.         this.adView = new AdView(this, AdSize.SMART_BANNER, "ID123ID123ID");
  18.         // Doing something I'm not 100% sure on, but guessing by the name //
  19.         adView.refreshDrawableState();
  20.         adView.setVisibility(AdView.VISIBLE);
  22.         // ADVIEW layout, show at the bottom of the screen //
  23.         final FrameLayout.LayoutParams adViewLayoutParams =
  24.                 new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
  25.                                              FrameLayout.LayoutParams.WRAP_CONTENT,
  26.                                              Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
  28.         // REQUEST an ad (Test ad) //
  29.         AdRequest adRequest = new AdRequest();
  30.         adRequest.addTestDevice("MY_TEST_DEVICE_CODE");
  31.         adView.loadAd(adRequest);
  33.         // RENDER the add on top of the scene //
  34.         this.mRenderSurfaceView = new RenderSurfaceView(this);
  35.         mRenderSurfaceView.setRenderer(mEngine, this);
  37.         // SURFACE layout ? //
  38.         final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
  39.                 new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
  41.         // ADD the surface view and adView to the frame //
  42.         frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
  43.         frameLayout.addView(adView, adViewLayoutParams);
  45.         // SHOW AD //
  46.         this.setContentView(frameLayout, frameLayoutLayoutParams);
  47.   } // End of onSetContentView() //
  48. } // End of CLASS //
Parsed in 0.014 seconds, using GeSHi

Add your device, if you are using one (I was, so I don't know about the emulator), as a test device :exclamation: . If you click your own ads, even just to test them, you will face the consequences :flag_red: .
To find out your device ID you must use the Old LogCat and follow these instructions.

When you need to show and hide your ads :lighbulb: : click here

That should have covered everything :? , but if you have anything else to add please do :P .
If anything is wrong please post it here and I will edit the post too ;) .
That is all I know, I hope it helps :lol: :D
Umz Games -:- Enter Into a New Reality -:-

Umz Mini Games on Google Play - Download Now
User avatar
Posts: 46
Joined: Thu Sep 20, 2012 12:55 am
Location: UK

Re: :-: Google AdMob in AndEngine Tutorial :-:

Postby Qkhang » Thu Feb 07, 2013 6:48 pm

I know that others may have mentioned this before, but it also happened to me. The first time you set it up, it may take a few minutes for the first ad to show up. In my case it took like 20 mins. During that 20 mins, Eclipse did not show any errors and there were no ads being displayed, so I panicked. I swore up and down in a confused state, checked my code and set up a millions times. I thought others were saying a few mins as in 5 min or less, but its been 20 min. In my confused state, I had left the test device on and the app running where the ad was supposed to show up and while searching my code and swearing, in the corner of my eye I saw a flicker on my test device….load and behold, there it was….the banner ad worked. There was nothing wrong, it just took a while for it to appear. Ever since then, its been working. Just thought I’d share this so that if it does happen to you, don’t panic, at least not right away.
Posts: 116
Joined: Fri Jan 20, 2012 7:00 pm

Re: :-: Google AdMob in AndEngine Tutorial :-:

Postby vasanthnagarajan » Tue Nov 26, 2013 4:36 pm

Hey I have a problem. I am using the above code and the ad is being displayed properly. But I want to show it only in gameover scene. I am not able to set visibility from gameover scene as the ad is declared in the Base game activity. can anyone help :o
Posts: 12
Joined: Thu Oct 24, 2013 8:38 am

Return to Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests