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

228 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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