Center MoPub baner on SurfaceView - android

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);

Related

How to move burstly ads to the center of the screen in android?

The MainActivity is extending UnityPlayerActivity.
We are using the following code snippet to place banner ads on android.
FrameLayout layout1 = new FrameLayout(this);
BurstlyAnimatedBanner levelUpBanner = new BurstlyAnimatedBanner( this, layout1, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT), "0455184989016204562", "Level_Up_Screen", 30, false);
LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
addContentView(layout1,lp1);
Doing this is showing the ads at the top-left corner of the screen. I need the ads at the top-center of the screen. I tried using layout1.setForegroundGravity(Gravity.CENTER); but that did not help. How can I achieve what I need? Also, I need to do this programatically and cannot use and xml.
I'm the developer of the Burstly Unity plugin at github.com/Burstly/BurstlyUnityPlugin.
You can center the banner ad by adding it to a RelativeLayout instead of a FrameLayout as we do in the plugin:
mBaseLayout = new RelativeLayout(mActivity);
mBaseLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mActivity.addContentView(mBaseLayout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.topMargin = (int)originY;
lp.leftMargin = (int)originX;
burstlyView.setLayoutParams(lp);
mBaseLayout.addView(burstlyView);
With this, you can then position the banner at will. If you wish to position it at the top center, then you'd want to change lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT) to lp.addRule(RelativeLayout.CENTER_HORIZONTAL).

Banner ad not occupying full width

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);

show admob ads at the bottom of the RelativeLayout using java no XML

I have designed a game with no xml layouts.I have used my own view extending the SurfaceView class .This is the code i am using.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
_ad=new AdView(this,AdSize.BANNER,"################");
rl.addView(myView);
rl.addView(_ad);
_ad.loadAd(new AdRequest());
setContentView(rl);
myView is the instance of class extending SurfaceView
and
_ad is the AdView instance.
I want to show the the ads at the bottom of the screen.But they are getting displayed only at the top.
Please suggest me what to do.I will be very grateful.
The only mistake I was doing was not associating my LayoutParams lay variable with the _ad AdView instance.So here is the code after modification.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
_ad=new AdView(this,AdSize.BANNER,"################");
rl.addView(myView);
rl.addView(_ad,lay); //line which was changed.Everything else was correct
_ad.loadAd(new AdRequest());
setContentView(rl);
Thank You StackOverflow.

adding other content in top and bottom of MySurfaceView

I am designing an android application contain MySurfaceView includes a Canvas to draw images in a thread running through the application,
(layout is default main.xml which create with project)
but i have to add a bar/textView in the top and add an AD in the botton of the screen.
then how can i do this ?
my declaration is like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}
May be my given information is not enough to understand the situation, so if you find any obscurity in my question please ask me
Thanks in advance
Hmm, one possibility is that you could just put it inside of a RelativeLayout or something similar? This assumes that you don't want to just define it in the xml which would probably look cleaner. The code would probably look something like this (you can definitely make it more efficient/just use a LinearLayout, I just threw down the first thing that came into my head):
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams paramsTop =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the textView to the top
paramsTop.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
TextView textView = new TextView(this);
textView.setText("Hello World");
// Give them id's since we need to position relative to one another
textView.setId(1);
layout.addView(textView, paramsTop);
AdView adView = new AdView(this, AdSize.BANNER, INSERT_ADMOB_ID_HERE);
adView.setId(2);
RelativeLayout.LayoutParams paramsBottom =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the adView to the bottom
paramsBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layout.addView(adView, paramsBottom);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
SurfaceView view = new SurfaceView( this );
view.setId(3);
RelativeLayout.LayoutParams paramsMiddle =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the surfaceView in between the textView and the adView
paramsMiddle.addRule(RelativeLayout.BELOW, textView.getId());
paramsMiddle.addRule(RelativeLayout.ABOVE, adView.getId());
layout.addView(view, paramsMiddle);
setContentView(layout);

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