Hello I want to integrate Admob in my LibGdx Game but the problem is if I do this my Game screen is stretching the Gameview I dont want this! i just want to overlap the Ad over the Gameview without stretching the gameview
Here is my Code:
protected AdView adView;
protected View gameView;
private InterstitialAd interstitialAd;
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
protected Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_ADS: {
adView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS: {
adView.setVisibility(View.GONE);
break;
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = false;
cfg.useCompass = false;
cfg.useImmersiveMode = true;
cfg.hideStatusBar = true;
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
AdView admobView = createAdView();
layout.addView(admobView);
View gameView = createGameView(cfg);
layout.addView(gameView);
setContentView(layout);
startAdvertising(admobView);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Toast.makeText(getApplicationContext(),
// "Finished Loading Interstitial", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed() {
// Toast.makeText(getApplicationContext(),
// "Closed Interstitial", Toast.LENGTH_SHORT).show();
}
});
}
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.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg) {
gameView = initializeForView(new Game(this), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
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);
adView.setVisibility(View.GONE);
}
@Override
public void showOrLoadInterstital() {
try {
runOnUiThread(new Runnable() {
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
// Toast.makeText(getApplicationContext(),
// "Showing Interstitial", Toast.LENGTH_SHORT).show();
} else {
AdRequest interstitialRequest = new AdRequest.Builder()
.build();
interstitialAd.loadAd(interstitialRequest);
// Toast.makeText(getApplicationContext(),
// "Loading Interstitial", Toast.LENGTH_SHORT).show();
}
}
});
} catch (Exception e) {
}
}
@Override
public void onResume() {
super.onResume();
if (adView != null)
adView.resume();
}
@Override
public void onPause() {
if (adView != null)
adView.pause();
super.onPause();
}
@Override
public void onDestroy() {
if (adView != null)
adView.destroy();
super.onDestroy();
}
@Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}
}
I had the same problem recently, it comes from the Layout you are using to display your AdView and your View. The ReLativeLayout will strech the GameView when an Ad is displayed. To avoid that use, a FrameLayout instead of the RelativeLayout. It's very similar.
In your onCreate()
you'll need this :
FrameLayout fLayout = new FrameLayout(this);
FrameLayout.LayoutParams fParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
admobView = createAdView();
View gameView = createGameView(config);
fLayout.addView(gameView);
fLayout.addView(admobView);
setContentView(fLayout);
startAdvertising(admobView);
Be careful to add the admobView
to the frameLayout
after adding the gameView
so your admobView
overlaps the gameView
.