Search code examples
androidopengl-eslibgdxrevmob

Revmob in Libgdx


Hello I am trying to integrated Banner Ad using RevMob in Libgdx. But it is not displaying for some reason.

I am using the following code.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_game_new);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    // cfg.useGL20 = false;

    final RelativeLayout gameLayout = new RelativeLayout(this);

    RevMobIntegration revmob = new RevMobIntegration(this);

    RelativeLayout bannerLayout = new RelativeLayout(this);


    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adParams.addRule(RelativeLayout.CENTER_VERTICAL);

    bannerLayout.setLayoutParams(adParams);

    game = new MyGdxGame(GameActivity.this, revmob);
    game.setRedirectionListener(this);


    View gameView = initializeForView(game, cfg);


    requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    // Add the libgdx view
    gameLayout.addView(gameView);
//        gameLayout.addView(bannerLayout);

    Log.d("RevMob", "Checking  BannerAd");
    if (revmob.getBannerAd() != null) {
        Log.d("RevMob", "Displaying Called");
        gameLayout.addView(revmob.getBannerAd());
    }


    // Hook it all up
    setContentView(gameLayout);


    this.onPause();
    this.onResume();
    gameLayout.refreshDrawableState();


    initChartboost();


//        startRevMobSession();

}

Here is the RevMobIntegration class :

  public class RevMobIntegration implements RevmobAdInterface {

   private static final String APPLICATION_ID = "YourAdmobAppIDHere";

    // Set this to false when creating the version for the store.
   private static final boolean DEBUG = true;

    private RevMobAdsListener listener;

  private RevMobFullscreen fullscreenAd;
   private RevMobBanner bannerAd;

   private Activity application;
   private RevMob revmob;

  public RevMobIntegration(Activity _application) {
  this.application = _application;

  startRevMobSession();
  }


  public void startRevMobSession() {
  //RevMob's Start Session method:
  revmob = RevMob.startWithListener(application, new RevMobAdsListener() {
     @Override
     public void onRevMobSessionStarted() {
        loadBanner(); // Cache the banner once the session is started
        Log.i("RevMob", "Session Started");
     }

     @Override
     public void onRevMobSessionNotStarted(String message) {
        //If the session Fails to start, no ads can be displayed.
        Log.i("RevMob", "Session Failed to Start");
     }
  }, application.getString(R.string.rev_mob_app_id));
   }


  //RevMob
   public void loadBanner() {
  bannerAd = revmob.preLoadBanner(application, new RevMobAdsListener() {
     @Override
     public void onRevMobAdReceived() {
        showBannerAd(true);
        Log.i("RevMob", "Banner Ready to be Displayed"); //At this point, 
      the banner is ready to be displayed.
     }

     @Override
     public void onRevMobAdNotReceived(String message) {
        Log.i("RevMob", "Banner Not Failed to Load");
     }

     @Override
     public void onRevMobAdDisplayed() {
        Log.i("RevMob", "Banner Displayed");
     }
  });
  }

   @Override
  public void showBannerAd(boolean show) {
  if(show) {
     Log.i("RevMob", "Showing");
     if(bannerAd == null) {
        startRevMobSession();
     } else {
        Log.i("RevMob", "Banner Displayed");
        bannerAd.show();
       }
     } else {
     bannerAd.hide();
  }
  }

  public RevMobBanner getBannerAd() { return bannerAd; }


 }

I have integrated the RevMob in my Activities and it is working fine. But for the Game Screen the ad is initializing but not displaying. Any suggestions?


Solution

  • It seems revmob.getBannerAd() return null because bannerAd object created when loadBanner(); called. RevMob take some time to start it's session.

    if (revmob.getBannerAd() != null) {
         Log.d("RevMob", "Displaying Called");
         gameLayout.addView(revmob.getBannerAd());
    }
    

    You can check this repo for clarification also you can take a look of this class.