Reputation: 566
I was able to add banner ads on my libgdx android app but I need some help position the add. Right now if I run it than the banner ads display at top/center and it move the rest of the app screen down.
How can I change it so that banner ads just floats at top/center without moving the app screen down?
public class AndroidLauncher extends AndroidApplication {
// Ads
private static final String AD_UNIT_ID_BANNER = "ca-app-pub-222222222/111111";
private static final String GOOGLE_PLAY_URL = "https://play.google.com/store/apps/developer?id=testname";
protected AdView adView;
protected View gameView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
AdView admobView = createAdView();
layout.addView(admobView);
View gameView = createGameView(config);
layout.addView(gameView);
setContentView(layout);
startAdvertising(admobView);
//initialize(new MyGdxGame(), config);
}
private AdView createAdView() {
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID_BANNER);
adView.setId(12345); // this is an arbitrary id, allows for relative
// positioning in createGameView()
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg) {
gameView = initializeForView(new MyGdxGame(), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, adView.getId());
gameView.setLayoutParams(params);
return gameView;
}
private void startAdvertising(AdView adView) {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Upvotes: 0
Views: 1321
Reputation: 11
change the view first and adview to second as given below in onCreate
View gameView = createGameView(config);
layout.addView(gameView);
AdView admobView = createAdView();
layout.addView(admobView);
And remove or hide this line from createGameView function
params.addRule(RelativeLayout.BELOW, adView.getId());
Upvotes: 1