1
votes

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.

1
give adView fixed heightuser13146129
but how? adaptive banner height is decided dynamically. there is no minimum height we can give as per docganesh101
Could you add some more code of the layout?user13146129

1 Answers

0
votes

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