Doflaminhgo
Doflaminhgo

Reputation: 577

Admob Banner in LinearLayout with Libgdx Screen

I am currently working on including admob ads into my first libgdx project. I already managed to show a banner with a relative layout. but i would like to know what it would look like in a vertical LinearLayout, where the gameScreen is added first and then the banner is added. All code examples regarding linear layouts i have looked at so far where implemented in xml, but i would like to implement it via code.

this is what my current implementation with relative Layout looks like:

RelativeLayout layout = new RelativeLayout(this);
        layout.addView(gameView);

    RelativeLayout.LayoutParams adParams =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layout.addView(adView, adParams);

    setContentView(layout);
}

I tried to replace every "RelativeLayout" wih "LinearLayout" and layout.setOrientation(LinearLayout.VERTICAL) but i guess thats not how LinearLayouts work. I only have little experience in Android app programming so i would appreciate any hints.

Edit: This is the (very basic) approach i tried myself (with and without the outcommented line, in both cases only the gameView is visible):

LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        //layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        layout.addView(gameView);
        layout.addView(bannerAdView);

        setContentView(layout);

Upvotes: 4

Views: 381

Answers (1)

Matthew Beck
Matthew Beck

Reputation: 359

    // Create the layout
    Linearlayout = new LinearLayout(this);

    // Create the libgdx View
    View gameView = initializeForView(new YourGame(), false);

    // Create and setup the AdMob view
    AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxx");
    adView.loadAd(new AdRequest());

    // Add the libgdx view
    layout.addView(gameView);

    // Add the AdMob view
    LinearLayout.LayoutParams adParams = 
        new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
                LinearLayout.LayoutParams.WRAP_CONTENT);

    layout.addView(adView, adParams);

    // Hook it all up
    setContentView(layout);

This is a modified version from https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx

Upvotes: 1

Related Questions