Rahul Bhavani
Rahul Bhavani

Reputation: 311

Admob Android: No callback to onAdLoaded after internet connection is established

I am using admob for ads. If I am connected to internet prior to opening the application, ad is loaded correctly but, if I start the application first without connecting to internet and after the app is started, if I connect to internet then there is no callback to onAdLoaded which fails in loading of Ads.

My Layout overview:

    <RelativeLayout 
        <layout_height>"match_parent"<layout_height>
        <layout_width>"match_parent"<layout_width>
            <RelativeLayout>
                <layout_height>"match_parent"<layout_height>
                <layout_width>"match_parent"<layout_width>   
                <layout_above>"adView"<layout_above>
                     <!--Main content--->
            </RelativeLayout>
            <com.google.android.gms.ads.AdView
                xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:visibility="gone"
                ads:adSize="BANNER"
                ads:adUnitId="@string/test_ad_id">
            </com.google.android.gms.ads.AdView>
    </RelativeLayout>

Java code:

    MobileAds.initialize(this, getString(R.string.test_ad_id));
    final AdView adView = findViewById(R.id.adView);
    final AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    adView.setAdListener(new AdListener() {

        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            adView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAdFailedToLoad(int i) {
            super.onAdFailedToLoad(i);
            adView.setVisibility(View.GONE);
        }
    });

This is weird because there is no callback to onAdLoaded on connecting to internet after the app is running. Does adView visibility is causing problem ? I believe that visibility is not a problem because initially the visibility of adView is gone. And as I stated earler if the app is connected to internet prior to starting, everything works fine.

Upvotes: 3

Views: 803

Answers (1)

Woods
Woods

Reputation: 101

I think you have to create OnConnectionEstablist event/function which will load the ad only if the internet is connected.

Upvotes: 1

Related Questions