Question

I followed the instruction on google and on libgdx wiki to integrate admob. But i am unable to see any change on my device.

Could you tell me if there is any obvious mistake that I have done. The following code is the mainActivity for my android project.

package com.chandings.pigswillfly;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AndroidApplication {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
        cfg.useGL20 = false;

        initialize(new MyGame(), cfg);*/

        String AD_UNIT_ID = "pub-4445095092775025";

        RelativeLayout layout = new RelativeLayout(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

        View gameView = initializeForView(new MyGame(), false);

        // Create and setup the AdMob view
        AdView adView = new AdView(this);//(this, AdSize.BANNER, "xxxxxxxx"); // Put in your secret key here

        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
        .addTestDevice("4D24C3467C244C5886FD0B1DA543A074")
        .build();

        // Add the libgdx view

        // Add the AdMob view
        RelativeLayout.LayoutParams adParams = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                                RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        adView.loadAd(adRequest);
        layout.addView(adView, adParams);
        layout.addView(gameView);

        // Hook it all up
        setContentView(layout);


    }
}
Was it helpful?

Solution

Well there were two mistake i was doing. One was very obvious the other was not.

  1. String AD_UNIT_ID = "pub-4445095092775025"; actually this is a publisher id and not ad unit id.

2. layout.addView(adView, adParams); layout.addView(gameView);

This will actually keep my game view over the adview thus adview will never be shown. the proper way of adding adview would be.

    layout.addView(gameView);
    layout.addView(adView, adParams);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top