GameCorder.net

このエントリーをはてなブックマークに追加

appear admob to center and bottom in android

how to appear admob in center and bottom of screen.
cocos2dx project has AppActivity and put code to AppActivity.

public class AppActivity extends Cocos2dxActivity {
    private static AppActivity _appActiviy;
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
				// (1)instantiate admob
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
				// banner id in string
        adView.setAdUnitId(getString(R.string.banner_ad_unit_id));

				// (2)request for admob
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("HASH_DEVICE_ID")
                .build();

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

        // (3)layout wrapper to center and bottom admob
        RelativeLayout relativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
                AdView.LayoutParams.MATCH_PARENT,
                AdView.LayoutParams.MATCH_PARENT);
        relativeLayout.setLayoutParams(adViewParams);

        // admob view
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
                AdView.LayoutParams.WRAP_CONTENT,
                AdView.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        adView.setLayoutParams(adParams);
        relativeLayout.addView(adView);
        Cocos2dxActivity.sContext.addContentView(relativeLayout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        _appActiviy = this;

    }

    public static void hideAd() {
        _appActiviy.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                if (_appActiviy.adView.isEnabled())
                    _appActiviy.adView.setEnabled(false);
                if (_appActiviy.adView.getVisibility() != View.INVISIBLE)
                    _appActiviy.adView.setVisibility(View.INVISIBLE);
            }
        });
    }

    public static void showAd() {
        _appActiviy.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!_appActiviy.adView.isEnabled())
                    _appActiviy.adView.setEnabled(true);
                if (_appActiviy.adView.getVisibility() == View.INVISIBLE)
                    _appActiviy.adView.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (adView != null) {
            adView.resume();
        }
    }

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

    @Override
    protected void onDestroy() {
        adView.destroy();
        super.onDestroy();
    }
}
		

(1)instanciate admob Adview is imported by gradle file
so we need to add code to build.gradle

(2)instantciate AdRequest.
Need AdRequest to show admob.

(3)Define layout wrapper to show center and bottom. Point is addRules to relativeLayout and it's layout to Cocos2dxActivity. Because Cocos2dXActivity is a parent class for AppActivity.

build.gradle

Next is build.gradle file.
Need admob import. It's in app's build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.firebase:firebase-ads:9.0.0'
    compile project(':libcocos2dx')
}
	

I confirm version 3.15.1.