Make AdView have a fixed size upon start of an activity

1.2k views Asked by At

Is there a way of forcing AdView to take it's initial size immediately upon activity start and stay at that fixed size. As it is now, it changes upon loading of the ads, meaning my renderers onsurfaceChanged function will be called twice upon starting the activity, which causes some initialization to be run twize.

I can work around this, but it would be very very much easier if there was a simple way to assign a fixed size to the banner on inflation. I would prefer not to hardcode dip values for the banner size. My AdView is initialized by the below xml:

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="unit_id"
        ads:loadAdOnCreate="true"
        ads:testDevices="device_id"
        android:visibility="visible" >
    </com.google.ads.AdView>

Device/unit ids obviously changed to protect the innocent (me)

2

There are 2 answers

1
user2072160 On BEST ANSWER

Found my own answer after some mucking about. It avoids the hardcoded values and lets me create the AdView purely in xml. Provided below for anyone interested:

I created this subclass of AdView:

public class MyAdView extends AdView {


public MyAdView(Activity activity, AdSize adSize, String adUnitId) {
    super(activity, adSize, adUnitId);
    // TODO Auto-generated constructor stub
}

public MyAdView(Activity activity, AdSize[] adSizes, String adUnitId) {
    super(activity, adSizes, adUnitId);
    // TODO Auto-generated constructor stub
}

public MyAdView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

public MyAdView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    Context ctx=getContext();
    AdSize testSize=AdSize.createAdSize(AdSize.SMART_BANNER, ctx); //Need to do this because the final static variable SMART_BANNER has buggy behaviour and returns a negative size if you don't.
    int height=testSize.getHeightInPixels(ctx);
    int width=testSize.getWidthInPixels(ctx);
    setMeasuredDimension(width, height);
}

}

And changed the xml view to (ids changed again):

    <packagename.MyAdView
        android:id="@id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="unitId"
        ads:loadAdOnCreate="true"
        ads:testDevices="deviceId"
        android:visibility="visible" >
    </packagename.MyAdView>

Including this in my layout, I now get a fixed size for my AdView, without any code.

0
Sunny On

May be setting height and width dynamically on inflation can solve your problem.

 adView.setLayoutParams(new LinearLayout.LayoutParams(width, height)); //Change LinearLayout to something else if your adview is in another layout