Reputation: 113
I want to display banner on top of the screen, but there is a problem. In my code (this is not my code, because I`m in learning process) I have two options. To show banner under or over the screen on bottom of screen.
public void showBanner(final boolean inLayout) {
//banner ad
if (BANNER_AD_UNIT_ID.length() > 0) {
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(BANNER_AD_UNIT_ID);
if (inLayout) {
//make ad visible on bottom of screen over surface
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
adView.setLayoutParams(params1);
} else {
//make ad visible on bottom of screen under surface
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
params.weight = 0;
adView.setLayoutParams(params);
}
If I change params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
from BOTTOM
to TOP
, banner is displayed over the screen on top. But if I change params.gravity = Gravity.BOTTOM;
from BOTTOM
to TOP
, ad is still showed on the bottom :(
Anyone can help me with this? Thanks so much!
inLayout code:
// Start loading the ad in the background.
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
public void onAdLoaded() {
View parent = (View) adView.getParent();
if (parent != null) {
if (!(parent.equals(layout) || parent.equals(linear_layout))) {
if (inLayout)
layout.addView(adView);
else
linear_layout.addView(adView);
recalculateScreen();
}
} else {
//add new banner ad to screen
if (inLayout)
layout.addView(adView);
else
linear_layout.addView(adView);
recalculateScreen();
}
}
});
}
}
//add relative layout to linear layout for banner ad mobility
linear_layout = new LinearLayout(this);
linear_layout.setOrientation(LinearLayout.VERTICAL);
linear_layout.addView(layout);
setContentView(linear_layout);
holder = surface.getHolder();
Upvotes: 1
Views: 2111
Reputation: 10869
First of all i think this code overall View
is a RelativeLayout
that has a LinearLayout
that contains some other View
s.. the first code aligns your addz to the bottom of the parent and the second code aligns it to the bottom of its container in the LinearLayout
view container -(idk if i make sense)
but your initial edit works because its being aligned relatively to the parent, and your second edit, re-define's the position of the addview in its container.. so to change it in the else code (assuming my logic is right)
//make ad visible on bottom of screen under surface
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
params.weight = 0;
adView.setLayoutParams(params);
// my addition starts here
//inLayout is linearlayout, that was what i thought but no.
ViewGroup parent = (ViewGroup) adView.getParent(); // your linear layout
if(parent.getChildCount() >0){
View viewToTake = parent.getChildAt(0); // taking the first child element
parent.remove(parent.indexOfChild(adView)); // you get the index of the adview layout and remove it
parent.addView(adView,0); // adding it at the first position
parent.addView(viewToTake,parent.getChildCount());// adding the view we took out back into play, you can decide to add it as the
// second element since it was your initial first with parent.addView(viewToTake,1);
EDIT 1 copy paste this and leave the first one..
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
public void onAdLoaded() {
View parent = (View) adView.getParent();
if (parent != null) {
if (!(parent.equals(layout) || parent.equals(linear_layout))) {
if (inLayout)
layout.addView(adView);
else
linear_layout.addView(adView,0); // adding it to linearLayout first element, that's what the zero does
recalculateScreen();
}
} else {
//add new banner ad to screen
if (inLayout)
layout.addView(adView);// same here
else
linear_layout.addView(adView,0); //same here, goes to the top
recalculateScreen();
}
}
});
}
} for the relative layou you can do the align parent stuff
Upvotes: 1