Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android admob resize on landscape

I have declared adMob on manifest like

<activity 
           android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          />

It is resized properly when device rotacion but now I must add android:configChanges="keyboardHidden|orientation" to activity that contains adMob in order to prevent activity reloading. I am reaching it but now adMob is not scaled when landscape. onConfigChanges event I could now force adMob to be resized with proper landscape dimensions. How? thank you.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
    }
like image 474
Jaume Avatar asked Jul 01 '12 11:07

Jaume


3 Answers

I faced the same problem, but found a work-around.

My AdView sits at the bottom of the page, inside of a Relative layout. It looks correct when loaded, so I assumed the LayoutParams were correct. To fix this issue, I had to remove everything from the RelativeLayout, create a new AdView, and add it all back in the correct order, with the original LayoutParams.

Something like:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    AdView adView = (AdView)findViewById(R.id.adView);
    View contentFrame = findViewById(R.id.contentFrameLayout);

    RelativeLayout parent = (RelativeLayout) adView.getParent();
    LayoutParams mapFrameParams = contentFrame.getLayoutParams();
    LayoutParams adViewParams = adView.getLayoutParams();

    parent.removeView(adView);
    parent.removeView(contentFrame);

    AdView newAdView = new AdView(this, AdSize.SMART_BANNER, getString(R.string.admob_pubId));

    newAdView.setId(R.id.adView);

    parent.addView(newAdView, adViewParams);
    parent.addView(contentFrame,mapFrameParams);
    newAdView.loadAd(new AdRequest());
}

This works for me, but still feels like a hack.

like image 105
Justin Smith Avatar answered Nov 17 '22 14:11

Justin Smith


Not enough Rep to reply directly but you are correct Justin Smith. As Eric Leichtenschlag states here https://groups.google.com/forum/#!msg/google-admob-ads-sdk/8Ch7VmAm7RE/34Tdw6Zz4jAJ the AdView needs to be recreated if you are using SMART_BANNER

What I did was remove the adView from the layout, destroy the adview, recreate it, add it back into the layout and then issued a AdRequest.

like image 44
CrosseyeJack Avatar answered Nov 17 '22 14:11

CrosseyeJack


Try this setup:

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/adkey"
    ads:loadAdOnCreate="false"
    android:focusable="false" />

The SMART_BANNER automatically selects the best size and type for your ad.

In your onConfigurationChanged method you would just request a new ad for the AdView.

Note that you might need to add xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads".

like image 2
nhaarman Avatar answered Nov 17 '22 13:11

nhaarman