Skip to content Skip to sidebar Skip to footer

Make Adview Have A Fixed Size Upon Start Of An Activity

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

Solution 1:

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:

publicclassMyAdViewextendsAdView {


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

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

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

publicMyAdView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}
@OverrideprotectedvoidonMeasure(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.

Solution 2:

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 

Post a Comment for "Make Adview Have A Fixed Size Upon Start Of An Activity"