Using a Java Interface

In this previous Lecture we've created a separate view that will take care of displaying the banner ads & in this section we'll add 2 methods to our AndroidLauncher for showing and hiding banner ads.
In order to use these methods from within our game's main class - which is located in our Core instead of our Android project - we will create a Java Interface. A Java Interface makes it possible to access these method from within our core project.

Creating a Java Interface in the Core project

Open up the SRC folder from the package explorer in your Core project and create a new Interface and give it the name AdsController.

Google AdMob Homepage

Add two methods:

  • showBannerAd
  • hideBannerAd
  • Your Interface should look like this:



    The methods in our Interface have empty bodies. The actual code for these methods isn't (and can't be) described in the interface, but in the class that's implementing this Interface.
    We're going to implement this interface in our AndroidLauncher.java file.
    Go back to the AndroidLauncher and add implements AdsController on the line of code where the class gets declared:

    When you import the AdsController you'll see that you'll get an error. Whenever a class implements an Interface it has to implement all of it's methods. So have your IDE add the unimplemented the methods for you.


    In Eclipse you can have add the unimplemented methods automatically by left-clicking on the lightbulb
    error notification and select "add unimplmented methods".
    In Android Studio it can be done with the CTRL+I shortcut.

    If all went well you should now have 2 methods with empty bodies added to the AndroidLauncher looking like this.


    Adding code to show the Banner Ad

    Now add the next piece of code to your showBannerAd method so it looks like this.


    What is happening here?
    First thing to know is that by default a LibGDX projects runs it's code on what is called the GL Thread, but for the AdMob related code to work it's mandatory that we run it on the UIThread. To run code on the UI thread we can use an android method called runOnUiThread which requires that we passed it a Runnable.
    A Runnable is an Interface which holds one method - run() - and within that method we define what code we want to run once we create that Runnable.

    In this case you see that we change the visibility of our AdView (named bannerAd) to VISIBLE (remember we set it to INVISIBLE when we created it?).
    AdRequest (line 7 & 8) is taking care of getting the ad from AdMob. And .loadAd(ad) is loading the ad into the AdView for display.
    Before we really get into showing the banner add on our devices lets first add the necessary code to hide the banner ad.

    Adding code to hide the Banner Ad

    Now add the next piece of code to your hideBannerAd method so it looks like this.


    What is happening here?
    Again we're creating a new Runnable that will run some code on the UI Thread.
    This time the code is more simple. The Ad has already been loaded and brought to view by setting the AdView's (=bannerAd) visibility to VISIBLE in our showBannerAd method.
    Now we just the VISIBILTY back to INVISIBLE wheneverwe don't want the ad to be displayed.
    The next time we want to display a banner ad again we call the showBannerAd method again and a new ad is loaded and shown.
    You could also simply set the visibility back to VISIBILE again (that is without creating a new AdRequest) and the same ad you'd shown before will be shown again. I'll leave that up to yourself to play around with it.

    Calling the showBannerAd / hideBannerAd methods from the Core project

    So now that we've written the necessary code to display and hide the banner ad we want to be able to call these methods from our Core project. In order to do that we're going to connect the AdsController implemented in our AndroidLauncher to our game's main class (MyGdxGame) so we can call both methods from the AndroidLauncher.
    We'll do this by creating a constructor in our game's main class to which the AdsController can be passed.
    The by the LibGDX's set up app auto-generated game's main class should look something like this:



    Now right above the @Override annotation of the create method add this code:



    We've now created a constructor to which we can pass the AdsController interface so that we can use it within our class.
    In the create() method add adsController.showBannerAd(); to get the banner ad shown.
    Only thing left to do is to actually pass the interface implemented by the AndroidLauncher class to MyGdxGame, so the showBannerAd your calling is actually running the code from your AndroidLauncher's method.
    You can do this by adding this as a parameter on the line of code where MyGdxGame is being initialized:



    Now you should be able to display banner adds. Run your Android project to test it .

    Links

    If you want to know more about Java Interface you can read more about the subject on f.e. the Java Documentation on the Oracle website:

  • What's an Interface?
  • The Runnable Interface

  • If you want to double-check if you added all the code correctly your AndroidLauncher.java, the AdsController.java interface and game's main class should like something like this: