简体   繁体   中英

admob ad banner overlapping my game screen android

i successfully integrated admob ads but i have an issue about placing banner ad under the screen of the game without overlapping the view of my game my current problem is like the picture Pic 1 and i want it to be like this picture Pic2

NB:im using java admob code this is my code

package com.xxxx.xxxxx;
import org.cocos2d.actions.CCActionManager;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSpriteFrameCache;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;
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.ideastudio.incroyableAventuresAlaadin.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

public class xxxxxx extends Activity{
/** Called when the activity is first created. */

public static xxxxx app;

public static CCGLSurfaceView mGLSurfaceView;
private boolean isCreated = false; 

private static final String AD_UNIT_ID = "ca-app-pub-xxxxxxxx";
private static final String AD_INTERSTITIAL_UNIT_ID = "ca-app-pub-xxxxx";

/** The interstitial ad. */
private InterstitialAd interstitialAd;
private AdView adView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    if( !isCreated ){
        isCreated = true;
    } else {
        return;
    }

    app = this;

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    RelativeLayout layout = new RelativeLayout(this);
    layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


    mGLSurfaceView = new CCGLSurfaceView(this);


    // Create the adView
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the adView to it
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM ,RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    adView.setLayoutParams(params);

    layout.addView(mGLSurfaceView);
    layout.addView(adView);

    setContentView(layout);


    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

    //-----------------------------------------------------Interstitial Add
    // Create an Interstitial ad.
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId(AD_INTERSTITIAL_UNIT_ID);
     // Load the interstitial ad.
    AdRequest interstitialAdRequest = new AdRequest.Builder().build();
    interstitialAd.loadAd(interstitialAdRequest);

    /////////////////////////////////////////////////////////////////////////////
    Common.game_initialize();
    getScaledCoordinate();
    CCDirector.sharedDirector().attachInView(mGLSurfaceView); 

    // attach the OpenGL view to a window
    Common.sound_engine = SoundEngine.sharedEngine();
    loadSound();

    CCScene scene = CCScene.node();
    scene.addChild(new HelloWorldLayer(), 1);
    CCDirector.sharedDirector().runWithScene(scene);
}

@Override
public void onStart() {
    super.onStart();
}

@Override    
public void onPause() {
    if (adView != null) {
          adView.pause();
        }


    super.onPause();

    MediaGlobal._shared().pauseMusic();

    if(GameLayer.sharedGameLayer() != null){
        GameLayer.sharedGameLayer().onPause(null);
    }
    CCDirector.sharedDirector().pause();
}

@Override
public void onResume() {
    super.onResume();

    if (adView != null) {
        adView.resume();
      }

    MediaGlobal._shared().resumeMusic();
}

@Override
public void onDestroy() {

    if (adView != null) {
          adView.destroy();
        }


    isCreated = false;

    MediaGlobal._shared().stopMusic();
    Common.sound_engine.realesAllEffects();

    super.onDestroy();
    CCDirector.sharedDirector().end();       

    CCTextureCache.sharedTextureCache().removeAllTextures();
    CCTextureCache.sharedTextureCache().removeAllTextures();
    CCSpriteFrameCache.sharedSpriteFrameCache().removeAllSpriteFrames();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK){
        exitGameDialog();
        return false;
    }
    return super.onKeyDown(keyCode, event);
}

public void exitGameDialog() {
    Builder builder = new AlertDialog.Builder(FlyingPanda.this)
            .setIcon(R.drawable.icon)
            .setTitle("Quitter le jeu?")
            .setMessage("Est-vous sûr?")
            .setNegativeButton("Non", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            })
            .setPositiveButton("Oui",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            CCActionManager.sharedManager()
                                    .removeAllActions();
                            CCDirector.sharedDirector().end();
                            finish();
                        }
                    });
    builder.create().show();
}

private void loadSound() {
    SoundEngine.purgeSharedEngine();

    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.bomb);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.bounce);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.death);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.fly);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.gamebg);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.gameover);
    Common.sound_engine.preloadEffect(CCDirector.sharedDirector().getActivity().getApplication(), R.raw.jumppad);
}

private void getScaledCoordinate() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    Common.SCREEN_WIDTH = displayMetrics.widthPixels;
    Common.SCREEN_HEIGHT = displayMetrics.heightPixels;
    Common.kXForIPhone = Common.SCREEN_WIDTH / 480.0f;
    Common.kYForIPhone = Common.SCREEN_HEIGHT / 320.0f;
}


// Admob Setting
////////////////////////////////////////////////////////////////////////////////

public void setHideAdView(final boolean bHide) {
    runOnUiThread(new Runnable() {
        public void run() {
            if(bHide) {
                adView.setVisibility(View.INVISIBLE);
            }
            else {
                adView.setVisibility(View.VISIBLE);
            }
        }
    });


}

public void showInterstitialAds()
{
    runOnUiThread(new Runnable() {
        public void run() {

            // Load the interstitial ad.
            AdRequest interstitialAdRequest = new AdRequest.Builder().build();
            interstitialAd.loadAd(interstitialAdRequest);

            interstitialAd.show();
        }
    });
}

  }

You need to add a rule for the Ad view, set it to "below" the gl surface view.

int id = 173; //random number
mGLSurfaceView.setId(id);
params.addRule(RelativeLayout.BELOW, id);

If using API level 17+ you can use:

View.generateViewId()

Or reserve that number in xml.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM