In this section we'll have a look at the AndroidLauncher.java file and add some code to display banner ads.
You also need to create an Ad Unit ID specific to your app.
Let's first do that by going to the AdMob webpage. If you sign in the AdMob console will give you an overview of which apps you have running AdMob on.
If you have any app set up, you can monetize your first by clicking the Monetize new app button.
This will bring you to a screen where you can enter your apps name under Add your app manuallyEnter your App's name and select Android as your platform and click on Add app
In Step 2 you can set up a Tracking ID for your app that can be used for app analytics. We're not going in to that in this tutorial so you can select Do not analyze this app.
At Step 3 you can select what ad format you want to use and you can change some of the ad unit's setting.
Make sure banner ad is selected and give your Ad Unit a name (you can leave all the default settings as they are).
At Step 4 AdMob gives you a link to the Google Developers site to view the set up instructions or gives an option to have them send to your email address.
The set up instruction vary slightly from how you should set up your code in a LibGDX project though, so you I suggest you just skip this step and click on Done.
To get the Ad Unit ID you could have either copied the Unit ID before you pressed Done :-).
or you can click on the Monetize tab on the AdMob console's Home screen.
Select the your app's name and in the Ad units tab you will see the name of your newly created Ad Unit with its Ad Unit ID
You can either select and copy the Ad Unit ID from this tab or click on your Ad Unit's name and copy the ID from the next page.
Once you've done that let's go back to your IDE and take a look at the AndroidLauncher.java file.
Now create a String that hold the Ad Unit ID's that we've just copied from the AdMob console (put it right above the @Override annotation).
We also need to declare that we want to use the AdView class from our Google Play Services library (I give it the name bannnerAd).
Let's look at our onCreate method.
When AndroidLauncher is run it will run our Game's starting class using the Android application configuration (
What we need in order to run banner ads is to have 2 views. 1 view that will take care of displaying the game and 1 view which will be on top of our first view and that is used for displaying banner adds.
So first create a View for the game that we'll name gameView and instead of initialize I'm going to use initializeForView.
Replace the line
initialize(new MyGdxGame(), config); with
We also need to create an AdView. Let's create a
setupAds() method that will take care of initializing the AdView and set some of its variables.
This method will be called right after my initializeForView code so make sure you add that call -
setupAds() - in the onCreate method.
In the setupAds method some variables of the AdView are being set:
The visibility is set to invisible by default (line 3), because I only want the ad to show up when I want to and not just immediately after I start my app.
Furthermore the AdView's background color is set to black, the Ad Unit ID is passed to the AdView and the AdSize is set to
Now that we have created two Views we still need to define their layout and how they're going to relate to each other.
For this we can use
You can add this piece of code to the onCreate method right after the setupAds method is called.
On line 1 we first create a new RelativeLayout object which is named
Then we add our gameView to the layout (line 2) and we set the width and height of this view to
MATCH_PARENT which will take in the width and height of our device.
On line 9 we add the AdView to our layout and pass in that views parameters which are set on line 5-8 to take the device's width (
WRAP_CONTENT to set the Views height to only be as high as it's content (which is the height of the Ad)
and add a parameter to align at the bottom of the screen.
Finally on line 11 we actually set the content view to the layout that we just defined.
Okay, so now that we have created the AdView and defined the layout of our Views let's add the code that display the ad.
We'll do that in the next section.
If you want to check your code to see if you added everything correctly this is what your AndroidLauncher.java should look like right now: