Admob ad does not correctly resize images when orienting the screen [Includes images]

I use Admob for advertising in my application and I am having a problem. Whenever I turn the screen into landscape mode, an ad appears, but it is the same size as in portrait mode. This problem arose after I added this xml declaration to my manifest to my main activity, which was necessary for the smooth operation of the main parts of the application:

android:configChanges="orientation|keyboardHidden|screenSize" 

I use a smart banner in my ad for size:

 ads:adSize="SMART_BANNER" 

I attached photos of this problem:

This is what it looks like in portrait mode. It runs perfectlyThis is what it looks like after I turn my phone sideways. The ad still shows but it doesn't resize to the fill width

What do I need to do to correctly modify the ad in landscape mode without deleting

 android:configChanges="orientation|keyboardHidden|screenSize" 

in the manifest for my main activity?

+4
source share
2 answers

this is how i solved it:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); orientation_changed = true; renewAd(); } private void renewAd() { AdView ad = (AdView) findViewById(R.id.adView); LayoutParams lp = (LayoutParams) ad.getLayoutParams(); // change relative layout for your layout which contains the adView RelativeLayout parent = (RelativeLayout) ad.getParent(); parent.removeView(ad); AdView newAd = new AdView(this, AdSize.SMART_BANNER, "YOUR ID"); newAd.setId(R.id.adView); newAd.setLayoutParams(lp); parent.addView(newAd); newAd.loadAd(new AdRequest()); } 

considers

+7
source

According to other answers (but updating them to the current AdMob SDK -v7.5- and providing the full code), the onConfigurationChanged () method for this activity should include destroying and creating the Ad View:

 // Remove the ad keeping the attributes AdView ad = (AdView) myactivity.findViewById(R.id.adView); LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ad.getLayoutParams(); LinearLayout parentLayout = (LinearLayout) ad.getParent(); parentLayout.removeView(ad); // Re-initialise the ad mAdView.destroy(); mAdView = new AdView(parent); mAdView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER); mAdView.setAdUnitId(myactivity.getString(R.string.banner_ad_unit_id)); mAdView.setId(R.id.adView); mAdView.setLayoutParams(lp); parentLayout.addView(mAdView); // Re-fetch add and check successful load AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice(parent.getString(R.string.test_device_id)) .build(); 
+6
source

All Articles