0
\$\begingroup\$

I have an AdMob banner in the game. But I need this banner not to be shown in Screen_1 and when user enter Screen_2 I need to show the banner. When I call AdView.gone() it dissapears from screen but coordinates becomes wrong and touch position doesn't match with coordinates. How can I properly remove AdView.

\$\endgroup\$
3
  • \$\begingroup\$ have you tried AdView.Remove()? \$\endgroup\$
    – Ryan white
    Aug 17, 2016 at 12:36
  • \$\begingroup\$ Yes, I tried that too but after that coordinates becomes wrong all the same. \$\endgroup\$
    – Alexander
    Aug 19, 2016 at 8:27
  • \$\begingroup\$ Try creating something that stores the cords, then call it when its called for creation. \$\endgroup\$
    – Ryan white
    Aug 19, 2016 at 12:14

1 Answer 1

1
\$\begingroup\$

First, in the class with the Activity, create a new Handler instance and override the method handleMessage(Message msg). Then, create two constants

private final int SHOW_ADS = 1;
private  final int HIDE_ADS = 0;

Pretty much, the Message has a variable called what with some information that you'll use for a switch line. This switch line will have to test if, what was passed in, is telling the method to display or hide the ad. It should look like this:

handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what){
                    case SHOW_ADS:
                        adView.setVisibility(View.VISIBLE);
                        break;
                    case (HIDE_ADS):
                        adView.setVisibility(View.GONE);
                        break;
                }
            }
        };

After you did that, you'll need to be able to call this from outside the main thread. The best way to call this method from outside the main thread is like this:

First, create a new interface with the method showAds(boolean show) and make AndroidLauncher implement it (or whatever your Main Activity is called)

public interface AdHandler{
    public void showAds(boolean show);
}

The overridden method in the class with the Main Activity (most likely AndroidLauncher) has to just relay the message to the handleMessage() method because you can't call the method directly:

@Override
    public void showAds(boolean show) {
        handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
    }

The final step is to pass the class itself into your main Gdx class

initializeForView(new MyGdxGame(this), config);

From there you can use the showAds() method whenever and wherever you want (you'll have to pass the variable around yourself).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .