Search code examples
android-studiolibgdxbanner-ads

Banner Ads (libgdx in Android studio)


I have a very difficult question for the community. I'm trying to insert a banner in a state of my application, but it is not the first one that opens at the beginning. In my application there is a initial screen and when I press in a hitbox, I go to the other screen with the banner, so in the following code TestPubb() is the second state.

This is the AndroidLauncher:

public class AndroidLauncher extends AndroidApplication {
private static final String TAG ="AndroidLauncher";
protected AdView adView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RelativeLayout layout = new RelativeLayout(this);

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    View gameView = initializeForView(new TestPubb(), config);
    layout.addView(gameView);


    adView = new AdView(this);
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Log.i(TAG, "Ad Loaded...");
        }
    });
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");


    AdRequest.Builder builder = new AdRequest.Builder();
    builder.addTestDevice("5845100B11B77FFC8C7AC54BD59AD27C");
    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    layout.addView(adView, adParams);

    adView.loadAd(builder.build());

    setContentView(layout);


}

}

What should I change?


Solution

  • Haven't you forgot to initialize the Mobile Ads SDK before loading a banner? This should be done once:

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    
    adView = new AdView(this);
    adView.setAdListener(new AdListener() {
    ...
    

    In more details described here