Admob bottom placement tutorial v3.0

After many failed tries I managed to position admob view at bottom.

You need to edit Cocos2dxActivity.java in libcocos2dx.

1.Add a new static member after private static Context sContext = null; :
public static FrameLayout framelayout;

2.In public void init() method change FrameLayout framelayout = new FrameLayout(this);
to framelayout = new FrameLayout(this);

In your AppActivity.java where you want to initialize admob use this:

` adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .build();
        
        RelativeLayout relativeLayout=new RelativeLayout(this);
        
        Cocos2dxActivity.framelayout.addView(relativeLayout);
       
        
        RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
        	    AdView.LayoutParams.WRAP_CONTENT,
        	    AdView.LayoutParams.WRAP_CONTENT);
        	//important
        	adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        	         
        	
        relativeLayout.addView(adView, adViewParams);
        
        
        adView.loadAd(adRequest);
        adView.setBackgroundColor(Color.BLACK);
        adView.setBackgroundColor(0);`
2 Likes

You may consider using FrameLayout instead of RelativeLayout/LinearLayout
Simply use FrameLayout by setting FrameLayout.Gravity as bottom.

@adrian_dsl
Hi, can you explain where I must change FrameLayout framelayout = new FrameLayout(this);
to framelayout = new FrameLayout(this). I have no public void init() method in Cocos2dxActivity.java
If it’s possible, post here sample Cocos2dxActivity.java.

Thank you!

@den1812 https://github.com/cocos2d/cocos2d-x/blob/v3/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
Take a look here.

@adrian_dsl I cannot make it work as your suggestion. Can you please have this in a sample project? Thanks a lot.

All, I can now have it worked as expected. Thanks a lot.

@adrian_dsl thank you very much fro sharing. In cocos2d-x 3.1 you have already mFrameLayout protected member defined. So you just need to write

 mFramelayout.addView(relativeLayout);

instead of

Cocos2dxActivity.framelayout.addView(relativeLayout);

Thank you again, I was looking for this for two days.

EDIT: The only problem is that it is in the bottom but it is not center aligned. How to align center in horizontal direction?

You can try to set the size of the banner as SMART_BANNER.

This topic is quite old and I’m sorry but I think this might help a few people.

When I tried to use this code I had severals problems. To solve them you only have to
“import android.widget.RelativeLayout;”

I hope this will help.

adView.setBackgroundColor(Color.BLACK);
adView.setBackgroundColor(0);

I’ve seen this before, but why do you set the background color to black, and then to 0?
Isn’t the black color line abundant now, as it’s overwritten immediately with 0?

Kind regards,
Michaël

I was directed to this thread by this one:
http://www.cocos2d-x.org/wiki/User_Tutorial-Integrate_AdMob

After much trial and error, I’ve finally created a working version from all the advice above. I’ve pasted the full working method below (in AppActivity.java) to make it easier. I’m using Cocos2d-x v.3.2.

There’s 2 approaches you can take. Both require you to use mFrameLayout.addView() to explicitly add a LinearLayout or RelativeLayout. You don’t use addContentView(adView,adParams) as in the original sample at the AdMob tutorial.
— RelativeLayout.LayoutParams does not have a gravity property so you use addRule()
— LinearLayout.LayoutParams has a gravity property that you set with Gravity.BOTTOM.

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState); 
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    _appActiviy = this;
  AdRequest adRequest = new AdRequest.Builder()
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
      .addTestDevice("B583486A9A64508744E7742E8CF3FC1F") 
      .build();
  adView = new AdView(this);
  // adView.setAdSize(AdSize.BANNER);
  // SMART_BANNER spans the full width of the screen
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);
    adView.loadAd(adRequest);
    adView.setBackgroundColor(Color.BLACK);
    adView.setBackgroundColor(0);  
    // Option 1 using Relative Layout
  //        RelativeLayout relativeLayout=new RelativeLayout(this);
  //        RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
  //	        AdView.LayoutParams.WRAP_CONTENT,
  //	        AdView.LayoutParams.WRAP_CONTENT);
  //        relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  //
  //        // nest the adView in the relativeLayout
  //        relativeLayout.addView( adView, relativeLayoutParams );
  //
  //        mFrameLayout.addView( relativeLayout );
    // Option 2 using Linear Layout with gravity
    LinearLayout linearLayout = new LinearLayout( this );
    LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
    		LinearLayout.LayoutParams.WRAP_CONTENT,
    		LinearLayout.LayoutParams.WRAP_CONTENT);
    adParams.gravity = Gravity.BOTTOM;
    linearLayout.addView( adView, adParams );
    mFrameLayout.addView( linearLayout );
    // no longer use this to add the view. Instead we're adding the linearLayout explicitly above.
    // addContentView(adView,adParams);

}

5 Likes

i tried the 2nd method using Cocos2d-x v 3.11. It works for me. Just want to let the rest know.

1 Like