Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob Adaptive Banner ad implementation

I am replacing my current banner ad with new Adaptive banner ads. Adaptive ad size is calculatedd after width size gets captured. I am placing framelayout of adview at bottom of layout.

<Relativelayout>
..
//// linear layout above ad_view_container   
<FrameLayout
        android:id="@+id/ad_view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true" />

</Relativelayout>

But now problem is, full Page gets loaded first, and after 1 -2 seconds ,whole layout shifted up and adative banner ad shows up.

So isnt this against the admob policy? how this case should be handled where i should set height for adaptive ads so this layout upshifting dont occur? searched lot but didnt find answer. Thanks in advance.

like image 909
ganesh101 Avatar asked Mar 05 '26 15:03

ganesh101


1 Answers

I'm not sure, if your only intent, is to ask, whether or not this is against the policies.

But to prevent the layout from changing after some time, I use the following implementation. It also helps, when you have a more complex layout composition like I do. I use a ConstraintLayout that splits my screen in two parts in portrait mode. The banner is within one of these two parts and the ratio between them is not fixed but depending on some logic. So this implementation also works with this requirement, as it overwrite onMeasure to determined the best size depending on the available width.

public class AdBanner extends FrameLayout
{
    private AdView mAdView;
    private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();

    public AdBanner(Context context)
    {
        super(context);
    }

    public AdBanner(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public AdBanner(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (!isShowBanner())
        {
            super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST));
            return;
        }
        int width = MeasureSpec.getSize(widthMeasureSpec);
        if (width <= 0)
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // That's where we determine the most accurate banner format.
        AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(getContext(), getDpWidth(width));
        int height = adSize.getHeightInPixels(getContext());
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode != MeasureSpec.UNSPECIFIED)
        {
            height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
        }
        setMeasuredDimension(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.getMode(heightMeasureSpec)));
    }

    protected int getDpWidth(int width)
    {
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        display.getMetrics(mDisplayMetrics);
        return (int) (width / mDisplayMetrics.density);
    }

    protected boolean isShowBanner()
    {
        // Do your checks here, like whether the user payed for ad removement.
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        if (!isShowBanner())
        {
            return;
        }
        int width = r - l;
        if (width <= 0)
        {
            return;
        }
        AdSize adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(getContext(), getDpWidth(width));

        // Prevent the ad from beeing added with each layout cicle,
        // by checking, whether or not available size actually changed the format of the banner
        if (mAdView == null || !adSize.equals(mAdView.getAdSize()))
        {
            removeAllViews();
            mAdView = new AdView(getContext());
            mAdView.setAdSize(adSize);
            ((GameApplication) getContext().getApplicationContext()).androidInjector().getAdService().loadBannerAd(getRootActivity(this), mAdView);
            this.addView(mAdView);
        }
        mAdView.layout(0, 0, width, b - t);
    }
}
like image 62
Aorlinn Avatar answered Mar 07 '26 05:03

Aorlinn