Ad is not coming at bottom - android

i have a problem regarding alignment of ad. The ad is coming at top of the app but i need it at the bottom of the app. I didnt defined any layout for ad just directly tried in java. And its working fine but only thing is its coming at top even after i inserted adView.setGravity(Gravity.BOTTOM);
The code goes as below
// Lookup R.layout.main
RelativeLayout layout = (RelativeLayout)findViewById(R.id.relativeLayout);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
AdView adView = new AdView(this, AdSize.BANNER, "a14e36b9902bfcc");
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
adView.setGravity(Gravity.BOTTOM);

You need to set the LayoutParams of your RelativeLayout to :
RelativeLayout.LayoutParams params=
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);

Specify the parameters when you add the view to the relative layout
Change
// Add the adView to it
layout.addView(adView);
to
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
layout.addView(adView,params);

Related

Best proactive when implementing adView

I am making a game and I would like the ad to be visible during the menu but not when the game is actually being played.
If I simple set the adView Visibility to INVISIBLE when I don't want it shown then the ad will not refresh and thus i don't think will register with admob.
Is there an accepted way of implementing ads like this?
edit* Code in main activity to add the adView
private void initialiseAd()
{
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("");
AdRequest adRequest = new AdRequest.Builder().build();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adView.setLayoutParams(lp);
//load ads
adView.loadAd(adRequest);
//add the adView to the window
mainLayout.addView(adView);
}
The way you're facing it is correct, you can easily hide the AdView simply wrapping it within a FrameLayout (which you already seem to be doing) and calling the setVisibility() method over it.
If you're using Activity, this would be the way:
FrameLayout containingFrame = (FrameLayout) findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);
If you're using Fragment:
FrameLayout containingFrame = (FrameLayout) getActivity().findViewById(R.id.ad_containing_framelayout_id);
containingFrame.setVisibility(View.GONE);

Sencha Touch - won't resize when AdView is added

I have this:
private void setupAds() {
AdView adView = new AdView(this, AdSize.BANNER, "a15138083566f58");
LinearLayout layout = super.root;
layout.addView(adView);
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
super.root.requestLayout();
}
When the ad loads, it covers up the bottom of my application. If I switch the orientation, everything re-renders correctly. This must mean that the WebView is the right size, but Sencha Touch isn't respecting the size change, right?
How do I fix this?
When adding an AdView programmatically to would normally add it into a ViewGroup that you have specially created and sized for that purpose. I have no idea what Sencha Touch is but if super.root is the ViewGroup for the whole Activity then your display will depend entirely upon what type of Layout what specified for the root View.
Suggest you create a AdViewParent in your layout and add the AdView into that.
The trick is to put adView not onto the super.root layout but onto the layout of WebView (appView of DridGap). Here is the code for horizontal align banner in bottom of the screen
private void setupAds() {
AdView adView = new AdView(this, AdSize.BANNER, "a15138083566f58");
//
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
AdSize.BANNER.getHeightInPixels(this));
rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, params);
//
appView.addView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
request adRequest = new AdRequest();
adView.loadAd(request);
}
Using this method you have to avoid overlapping of content by admob banner. You can do it in app.scss, someting like
#bottomitemid{
padding-bottom: 50px;
}

Help Adding another view to GLSurfaceView

I am trying to add another view to my GlSurfaceView however I keep kicking up errors.
glView = new GLSurfaceView(this);
glView.setRenderer(this);
setContentView(glView);
glView.setId(932203934);
int newID = glView.getId();
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, "a14e3ef0948eb58");
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
//LinearLayout layout = (LinearLayout)findViewById(newID);
GLSurfaceView layout = (GLSurfaceView)findViewById(newID);
// Add the adView to it
this.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
I have added "setID" as the glView always gave -1, but now with this I get "java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
Clearly I am not doing this right so how do I had this additional view to the glsurfaceview?
Thanks
Anyone else having this problem....
AdView adView = new AdView(this, AdSize.BANNER, "a14e3ef0948eb58");
LinearLayout ll = new LinearLayout(this);
ll.addView(adView);
this.addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);

Center MoPub baner on SurfaceView

I'd like add MoPub ad in my Android game. Game is created on SurfaceView Class. I have no xml layout file. The problem is that I can't center ad on screen. I try center and gravity everything. Still doesn't work. MoPubView extends FrameLayout.
MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId("_MY__ID_");
mAdView.loadAd();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout mRelativeLayout = new RelativeLayout(this);
mRelativeLayout.setGravity(Gravity.CENTER_HORIZONTAL);
mRelativeLayout.addView(mSurface);
mRelativeLayout.addView(mAdView, layoutParams);
mRelativeLayout.invalidate();
setContentView(mRelativeLayout);
Please advise me something because it drawing me crazy. I feel I'm missing something easy.
You can't center the content directly in MoPub, or at least I haven't found a way to do so. It's based on FrameLayout.
You have to center the ad in HTML. If you are using custom HTML code inside your MoPub campaign, just wrap the code inside <div style="text-align:center">...</div>. (Do this through the MoPub administration web). It solved my problem, I was using an OpenX ad provider.
Just create a container layout with "FILL_PARENT" parameters and add your ad view into it like this:
MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(XXXXXXX);
RelativeLayout adContainer = new RelativeLayout(this);
addContentView(adContainer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
LayoutParams adParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1); //to align vertically at top
adParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1); //to align horizontally at center
adContainer.addView(mAdView, adParams);
mAdView.loadAd();
I know it is not good idea to set constant banner width but as long as your banners are 320px it works fine:
final float scale = getResources().getDisplayMetrics().density;
MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(MOPUB_ID);
mAdView.loadAd();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (320*scale), LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout mRelativeLayout = new RelativeLayout(this);
mRelativeLayout.addView(mSurface);
mRelativeLayout.addView(mAdView, layoutParams);
mRelativeLayout.invalidate();
setContentView(mRelativeLayout);
I have done same thing with below code snippet.I am using LibGDX java game engine.
Relativelayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
moPubView = new MoPubView(mActivity.getApplicationContext());
moPubView.setAdUnitId("XXXXXX");// Banner test Ad unit Id.
moPubView.setAutorefreshEnabled(true);
moPubView.setVisibility(View.GONE);
layout.addView(moPubView,adParams);
1.I have Relative layout in parent.
2.Added MoPubView in the parent Relative layout with layoutParams only ALIGN_PARENT_BOTTOM && CENTER_HORIZONTAL
layout.addView(moPubView,adParams);

Admob on surfaceview

I have an activity which has a surfaceview capturing whole screen. i want to put an admob ad on surfaceview. I found this link but it does not work. Any idea ?
In your activity's onCreate:
// Add admob ads.
admobView = new AdView(this, AdSize.BANNER, "YOUR_UNIT_ID");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
admobView.setLayoutParams(lp);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(surfaceView);
layout.addView(admobView);
admobView.loadAd(new AdRequest());
setContentView(layout);
IN the example above, "surfaceView" is your surfaceView instance, that you should instantiate like you do today.
Don't forget to call admobView.destroy() in your activity's onDestroy.

Categories

Resources