Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob ad not resizing correctly upon screen orientation [Includes Pictures]

I am using Admob for ads in my app and I ran into a problem. Whenever I turn the screen to landscape mode, the ad shows up but it's the same size as it was in portrait mode. This problem occured after I added this xml declaration in my manifest to my main activity that was necessary to keep the main parts of the app functioning smoothly:

android:configChanges="orientation|keyboardHidden|screenSize"

I am using smart banner in my ad for the size:

ads:adSize="SMART_BANNER"

I have attached pictures 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 have to do to get the ad to resize properly in landscape mode without deleting

android:configChanges="orientation|keyboardHidden|screenSize"  

in the manifest for my main activity?

like image 475
user3131263 Avatar asked Dec 04 '22 08:12

user3131263


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

regards

like image 106
frapeti Avatar answered Dec 10 '22 13:12

frapeti