Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

java - 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 perfectly This 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...