0
\$\begingroup\$

I've recently started learning Java for android development. My first game is almost ready to publish in Google Play, but I'm affraid that google might say that on the one screen, the ad may be clicked accidentally, So I would like to hide it on that screen. The thing is, that Adview is initiated only in AndroidLuncher class in android project, but all the other classes are in core project. So what I'm asking for is a way to instruct Admob to hide when player enters GameScreen. Is that possible?

I could provide some code, but I don't know which part could help you, so I'll just post the AndroidLauncher Class onCreate method:

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

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.useAccelerometer = false;
    config.useCompass = false;


    RelativeLayout layout = new RelativeLayout(this);

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

    // Create the libgdx View
    View gameView = initializeForView(new MainClass(), config);


    AdView AdView = new AdView(this);
    AdView.setAdSize(AdSize.SMART_BANNER);
    AdView.setAdUnitId("ca-app-pub-SECRETCODE/HERE"); //The AdUnitId
    if(isNetworkAvailable())
    {
        AdView.setBackgroundColor(Color.BLACK);
    }
    AdRequest.Builder adRequest = new AdRequest.Builder();
    adRequest.addTestDevice("HERE_TOO"); //Test device
    AdView.loadAd(adRequest.build());


    layout.addView(gameView);

    // Add the AdMob view
    RelativeLayout.LayoutParams adParams = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    layout.addView(AdView, adParams);

    // Hook it all up
    setContentView(layout);

    AdView.setVisibility(View.VISIBLE);
}

This code works just fine, so I would prefer not to change it, as I've spent many hours getting it to work :)

\$\endgroup\$
0
\$\begingroup\$

If you ask me it is not a good approach. Good news is you don't have to change too much stuff. Create an ads controller interface in your core module and implement it in your main Android class. So you can pass it to your game class and store it. Then whenever you want you can call adsController.hideBannerAd(); But you should first implement that method :D

EDIT tutorial for how to do it: http://www.norakomi.com/tutorial_admob_introduction.php#scroll_introduction

\$\endgroup\$
  • \$\begingroup\$ Okay, so I tried with this: github.com/libgdx/libgdx/wiki/Admob-in-libgdx#control but when I try using "myRequestHandler.showAds(false);", it doesn't work :/ \$\endgroup\$ – urban07 Aug 18 '15 at 12:05
  • \$\begingroup\$ I've also tried an infinite loop inside AndroidLauncher.java that checks a variable in main core class, but it doesn't work either. I'll try running a seperate thread for this goal. \$\endgroup\$ – urban07 Aug 18 '15 at 12:36
  • \$\begingroup\$ @urban07 you don't need a separate thread I will provide you a link which explains what you should do. \$\endgroup\$ – Arda Kara Aug 18 '15 at 14:25
  • \$\begingroup\$ Okay, nvm - this article helped me a lot: norakomi.com/tutorial_admob_part2_banner_ads2.php I guess this is what you meant :) EDIT: woah, this is basically the same link :D Thanks for giving me a hint! \$\endgroup\$ – urban07 Aug 18 '15 at 14:27
  • \$\begingroup\$ Hope you finish your game 👍 \$\endgroup\$ – Arda Kara Aug 18 '15 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.