I am trying to place a Banner to my game, but when I execute it, the Banner is shown and not games, when I modify the code, the game is shown and not the Banner, this is my "MainAvtivity.java" code :
NOTE: Search the internet for some solutions and tell me that I should modify the variable "setContentView (view);" showing the game and the variable "setContentView (layout);" showing the Banner.
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends Activity {
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
MainView view;
public static final String WIDTH = "width";
public static final String HEIGHT = "height";
public static final String SCORE = "score";
public static final String HIGH_SCORE = "high score temp";
public static final String UNDO_SCORE = "undo score";
public static final String CAN_UNDO = "can undo";
public static final String UNDO_GRID = "undo";
public static final String GAME_STATE = "game state";
public static final String UNDO_GAME_STATE = "undo game state";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
view = new MainView(getBaseContext());
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
view.hasSaveState = settings.getBoolean("save_state", false);
if (savedInstanceState != null) {
if (savedInstanceState.getBoolean("hasState")) {
load();
}
}
setContentView(view);
//Add this in OnCreate of Activity to initialize the ad
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544/6300978111");
//Add this wherever your code needs to add the ad
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//Additionally to adjust the position to Bottom
layout.setGravity(Gravity.BOTTOM);
// Create a banner ad
AdView mAdView = new AdView(this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Add the AdView to the view hierarchy.
layout.addView(mAdView);
// Start loading the ad.
mAdView.loadAd(adRequestBuilder.build());
setContentView(layout);
AdRequest adRequest = new AdRequest.Builder().build();
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
...
Thank you and I hope for a quick solution.