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
569 views
in Technique[技术] by (71.8m points)

android - Admob Adaptive Banner ad implementation

I am replacing my current banner ad with new Adaptive banner ads. Adaptive ad size is calculatedd after width size gets captured. I am placing framelayout of adview at bottom of layout.

<Relativelayout>
..
//// linear layout above ad_view_container   
<FrameLayout
        android:id="@+id/ad_view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true" />

</Relativelayout>

But now problem is, full Page gets loaded first, and after 1 -2 seconds ,whole layout shifted up and adative banner ad shows up.

So isnt this against the admob policy? how this case should be handled where i should set height for adaptive ads so this layout upshifting dont occur? searched lot but didnt find answer. Thanks in advance.


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

1 Answer

0 votes
by (71.8m points)

For this you can used below code

public class MainActivity extends AppCompatActivity {

private FrameLayout adContainerView;
private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initialize the Mobile Ads SDK.
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) { }
    });
    adContainerView = findViewById(R.id.ad_view_container);
    // Step 1 - Create an AdView and set the ad unit ID on it.
    adView = new AdView(this);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    adContainerView.addView(adView);
    loadBanner();
}

private void loadBanner() {
    // Create an ad request. 
    AdRequest adRequest =
            new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .build();

    AdSize adSize = getAdSize();
    // Step 4 - Set the adaptive ad size on the ad view.
    adView.setAdSize(adSize);

    // Step 5 - Start loading the ad in the background.
    adView.loadAd(adRequest);
}

private AdSize getAdSize() {
    // Step 2 - Determine the screen width (less decorations) to use for the ad width.
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float widthPixels = outMetrics.widthPixels;
    float density = outMetrics.density;

    int adWidth = (int) (widthPixels / density);

    // Step 3 - Get adaptive ad size and return for setting on the ad view.
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}
}

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

...