i am able to center my banner using gravity class, but i am not able to stretch my banner to occupy full width.
And also i tried smart_banner which supposed to fill according to the density, but with no success.
need help
RelativeLayout layout = (RelativeLayout)findViewById(R.id.ultimaterellayout);
// Create the adView
AdView adView = new AdView(MainActivity.this, AdSize.BANNER, "aaa324dsfdfsd");
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
layout.addView(adView, layoutParams);
layout.invalidate();
// layout.addView(adView);
adView.loadAd(adRequest);
Try to set the width of RelativeLayout as fill_parent.
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
Related
I have an android game that draws on a surfaceView and I have admob ads on top of that using a FrameLayout.
FrameLayout lay = new FrameLayout(this);
this.addContentView(lay, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxx");
lay.addView(renderer, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lay.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
adView.setGravity(Gravity.LEFT);
this.setContentView(lay);
adView.loadAd(new AdRequest());
and everything is fine. I want to integrate the new google play services admob and I have changed the above code to
FrameLayout lay = new FrameLayout(this);
this.addContentView(lay, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
adView = new AdView(this);
adView.setAdUnitId("xxxxxxx");
adView.setAdSize(AdSize.BANNER);
lay.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.setContentView(lay);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
the admob view is not shown up. I know that it is the view that does not come up because if i add only the AdView into the frameLayour then I see the ads without any problem
Has anyone came across this problem? Has anyone any suggestion about that?
king regards
Make sure you set a background color for the adview. It can be "transparent". Then, using a RelativeLayout or a FrameLayout as your parent layout, define the layout parameters for the adView to be positioned, for example at the bottom center of the screen like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
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);
layout.addView(renderer);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}
I tried to display an ad on top of a custom view but somehow I can't see it.
layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.HORIZONTAL);
view = new GameView(this);
adView = new AdView(this);
adView.setAdUnitId("id");
adView.setAdSize(AdSize.BANNER);
layout.addView(view);
layout.addView(adView, 640, 100);
adView.loadAd(new AdRequest.Builder().addTestDevice("id").build());
setContentView(layout);
I think it has something to do with the way you're adding Views to the LinearLayout. The adView seems ok, but my bet is there's something with the GameView class that makes it not show up.
I'd recommend defining the AdView containing LinearLayout in the layout file instead of declaring a new LinearLayout programmatically, and add the View afterwards like you're doing right now. The following code is the one I use and it works, that's why I assume this is a layout problem.
final LinearLayout adLayout = (LinearLayout) findViewById(R.id.your_ad_containing_linearlayout);
final AdView adView = new AdView(this);
adView.setAdUnitId("your-id");
adView.setAdSize(AdSize.BANNER);
adLayout.addView(adView);
final AdRequest.Builder adReq = new AdRequest.Builder();
adReq.addTestDevice("...");
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);
I think the problem is the LinearLayout.
If you want to display the ad on top of the GameView, you should use FrameLayout simply like below
FrameLayout layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
view = new GameView(this);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
AdView adView = new AdView(this);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
adView.setLayoutParams(params);
adView.setAdUnitId("id");
adView.setAdSize(AdSize.BANNER);
layout.addView(view);
layout.addView(adView, 640, 100);
adView.loadAd(new AdRequest.Builder().addTestDevice("id").build());
I think the problem is that you set your 'parent' layout's orientation as Horizontal. Try set it as Vertical.
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
layout.setOrientation(LinearLayout.VERTICAL);
And make sure your custom view GameView doesn't have the layout height as MATCH_PARENT, because add it first, and if it's height is set to fill the parent the view below it won't be visible.
hello how to show mopub ad at the bottom?
I have this code
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mopView = new MoPubView(this);
mopView.setAdUnitId(PUB_ID_320x50);
mopView.setLayoutParams(params);
mopView.loadAd();
mainRelative.addView(mopView, params);
but it show in center of screen , how to align to bottom?
p.s. my main layout is RelativeLayout
//change your layout height to ill parent
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
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);
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);