Search code examples
javaandroidgitlibgdxadmob

How can I implement AdMob Ads in a LibGDX Android project?


I found this Flappy Bird GIT project: https://github.com/barodapride/flappy-tutorial-barodapride How can I implement the BannerAd in this? There is no layout file. And how can I implement the InterstitialAd? If I try to write

public InterstitialAd mInterstitialAd;

Then it says "cannot resolve symbol InterstitialAd"

I know how to do this in a normal Android Studio project, but this is the first time I'm working with libGDX and this is so complicated...


Solution

  • I answered here, how you integrate AdMob banner Ad inside your LibGDX project.

    Now I answer InterstitialAd integration after Integration of banner Ad.

    private static final String AD_UNIT_ID_INTERSTITIAL = "ca-app-pub-XXXXX/XXXXX";
    
    private InterstitialAd interstitialAd;
    
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        ...
        ...
    
        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
        interstitialAd.setAdListener(new AdListener() {
              @Override
              public void onAdLoaded() {}
    
              @Override
              public void onAdClosed() {
                 loadIntersitialAd();
              }
        });
    
        loadIntersitialAd();
    }
    
    
    private void loadIntersitialAd(){
    
        AdRequest interstitialRequest = new AdRequest.Builder().build();
        interstitialAd.loadAd(interstitialRequest);
    }
    
    @Override 
    public void showOrLoadInterstitial() {
    
         runOnUiThread(new Runnable() {
                public void run() {
                    if (interstitialAd.isLoaded()) 
                         interstitialAd.show();                         
                    else 
                         loadIntersitialAd();           
                }
          });
    }
    

    Need an interface to call showOrLoadInterstitial from core module so I created IActivityRequestHandler inside core module and implement this interface to AndroidLauncher of android module.

    public interface IActivityRequestHandler {
    
         void showOrLoadInterstitial();
    }
    

    EDIT

    You can't call non-static method, showOrLoadInterstitial() with class/interface name, need an object of IActivityRequestHandler implemented class so create a parameterized constructor of MyGdxGame and pass reference from android module.

     public class AndroidLauncher extends AndroidApplication implements IActivityRequestHandler  {
    
       @Override
       protected void onCreate (Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
    
           View gameView=initializeForView(new MyGdxGame(this));
           ....
           ....
       }
    

    Capture reference of IActivityRequestHandler inside MyGdxGame Game class

    public class MyGdxGame extends Game {
    
       Public IActivityRequestHandler requestHandler;
    
       public MyGdxGame(IActivityRequestHandler requestHandler){
           this.requestHandler=requestHandler;
       }
    
       ...
    }
    

    Now you've object reference, can called requestHandler.showOrLoadInterstitial()