Reputation: 39881
This code below positions the AdMob banner on the bottom of the view. But the problem is that I have used LinearLayout
and moved the banner up by 75 pixels. This is bad, as the banner height is usually 50px, but after moving it 50px up then it shows only part of the banner. Another problem is that the banner height is not fixed.
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(screenWidth, 2 * screenHeight - 75);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// 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()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
// Adding full screen container
addContentView(adView, adParams);
I have tried to use RelativeLayout
instead of LinearLayout
like this:
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
..................
......................
addContentView(adView, lay);
But this shows the banner on the top of the screen. How can I bring it to the bottom?
Upvotes: 2
Views: 6987
Reputation: 39881
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
RelativeLayout relativeLayout = new RelativeLayout(this);
mFrameLayout.addView(relativeLayout);
RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
AdView.LayoutParams.WRAP_CONTENT,
AdView.LayoutParams.WRAP_CONTENT);
// align bottom
adViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// align center
adViewParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
relativeLayout.addView(adView, adViewParams);
adView.loadAd(adRequest);
adView.setBackgroundColor(Color.BLACK);
adView.setBackgroundColor(0);
Where mFrameLayout
in my case is defined in cocos2d-x Cocos2dxActivity
class and is defined as follows:
// FrameLayout
ViewGroup.LayoutParams framelayout_params =
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mFrameLayout = new FrameLayout(this);
mFrameLayout.setLayoutParams(framelayout_params);
Upvotes: 4
Reputation: 20196
If you want the AdView to be positioned at the bottom of the screen, then a simple way of doing it it to wrap the content you want to display in a LinearLayout and mark it as android:Layout_weight="1"
. This will ensure that it consumes all space other than that consumed by your AdView.
<!-- In your layout xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- NB make layout_weight=1 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
.. your content goes here.
</LinearLayout>
<LinearLayout
android:id="@+id/adViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</LinearLayout>
Now in your onCreate()
setContentView(r.layout.layouts.);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Adding AdView to container
final ViewGroup adViewContainer = (ViewGroup) findViewById(R.id.adViewContainer);
adViewContainer.addView(adView);
// 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()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
Upvotes: 2
Reputation: 135
From the Android documentation, RelativeLayout.ALIGN_PARENT_BOTTOM
is a "Rule that aligns the child's bottom edge with its RelativeLayout parent's bottom edge," so the ad banner will be placed at the bottom of the RelativeLayout, not necessarily the bottom of the screen. To ensure that the banner is placed at the bottom of the screen, you need to extend the RelativeLayout to fill the screen, and you can do this by changing both RelativeLayout.LayoutParams.WRAP_CONTENT
to RelativeLayout.LayoutParams.MATCH_PARENT
.
EDIT: This only makes the banner appear at the bottom of the parent of the RelativeLayout, so make sure that the RelativeLayout has its bottom edge at the bottom of the screen.
As for using a LinearLayout instead, I suggest a RelativeLayout as it is much more versatile, can house LinearLayouts, and is probably the easiest if not only way to solve this specific problem.
To address your concern about the variable banner height, the height you set it to is in density-independent pixels (dp), so it will only vary between devices, not between multiple launches of the application on the same device (or emulator). See this page for all AdSizes and their explanations.
Upvotes: 0