Вопрос:

Error showing Admob Banner in Android Studio

android admob banner

359 просмотра

1 ответ

6 Репутация автора

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 code of the "MainAvtivity.java":

NOTE: Search the internet for some solutions and tell me that I must 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();
        }
    }

    ...

Thanks and I hope prompt solution.

Автор: Leo Источник Размещён: 08.11.2017 11:00

Ответы (1)


1 плюс

2320 Репутация автора

You should not call setContentView() for both view and layout, only setContentView for your game. Your MainView's root layout should be RelativeLayout or FrameLayout and add AdView to bottom, top or wherever you wish of your your root layout using layout parameters.

Автор: Thracian Размещён: 08.11.2017 11:27
Вопросы из категории :
32x32