Jaume
Jaume

Reputation: 3780

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);
    }

Upvotes: 7

Views: 7362

Answers (3)

nhaarman
nhaarman

Reputation: 100388

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

Upvotes: 2

CrosseyeJack
CrosseyeJack

Reputation: 154

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.

Upvotes: 4

Justin Smith
Justin Smith

Reputation: 433

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.

Upvotes: 11

Related Questions