MAGS94 MAGS94 - 3 months ago 8
Android Question

Position of ads on an android app

I have a libgdx game and I successfully put ads in it but I have one problem

The Ads covers the bottom part of screen where I have some important stuff

My question is How can I put both views (gameView and adView) so that they don't overlap each other ?

Here is AndroidLauncher class

public class AndroidLauncher extends AndroidApplication implements AdsController {

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 GameMain(this), config);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);

adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setVisibility(View.INVISIBLE);
adView.setBackgroundColor(0xff000000);
adView.setAdUnitId("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");


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

layout.addView(adView, adParams);

setContentView(layout);
}

@Override
public void showBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
adView.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
adView.loadAd(builder.build());
}
});
}

@Override
public void hideBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
adView.setVisibility(View.INVISIBLE);
}
});
}

@Override
public boolean showAds() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return (ni != null && ni.isConnected());
}
}


Here is AdsController interface

public interface AdsController {

public void showBannerAd();
public void hideBannerAd();
public boolean showAds();

}


and one more thing I want my game to take the full screen size if there is no internet connection (no ads).

How can I do this ?

Answer

Your game View needs layout params that set it above the AdView. Therefore, it needs RelativeLayout.LayoutParams, not just ViewGroup.LayoutParams. With this kind of layout, you can cause the game to fill the whole screen by setting the AdView's visibility to GONE instead of INVISIBLE. So you can just leave it gone until an ad is available.

To set it in relation to the other view, you need to give the other view an ID, which you can generate with View.generateViewId().

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

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        View gameView = initializeForView(new GameMain(this), config);

        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setVisibility(View.GONE);
        adView.setBackgroundColor(0xff000000);
        adView.setAdUnitId("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

        int adViewId = View.generateViewId();
        adView.setId(adViewId);

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

        RelativeLayout.LayoutParams gameParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        gameParams.addRule(RelativeLayout.ABOVE, adViewId);

        RelativeLayout layout = new RelativeLayout(this);
        layout.addView(adView, adParams);
        layout.addView(gameView, gameParams);

        setContentView(layout);
    }
Comments