Overview
In some Android apps you need to take action when an AdMob ad successfully loads and a different action when it does not. This post shows how to detect both cases using AdView.setAdListener and how to avoid requesting ads when there is no network connection.
The original examples and links are preserved below, followed by a short checklist and an updated example to match newer Google Mobile Ads SDK patterns.
Detecting ad load with AdListener (original example)
Use AdView.setAdListener and override onAdLoaded() and onAdFailedToLoad(…). This is the code from the original post:
ad.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(TAG, "Ad Received");
//Code implementation if the ad is loaded
………
………
}
@Override
public void onAdFailedToLoad(int errorcode) {
Log.d(TAG, "Ad Not Loaded Received");
//Code implementation if the ad is NOT loaded
………
………
}
});
Explanation
- onAdLoaded(): called when the ad has been successfully loaded into the AdView — put the code you want executed on success here (e.g., show the ad container).
- onAdFailedToLoad(…): called when the ad request failed — put fallback behavior here (e.g., hide the ad container, log the error, schedule a retry).
Note: the snippet above is the original form used in the article. Later SDK versions changed the parameter type for onAdFailedToLoad; see the updated example below.
Check network connectivity before requesting ads
The article recommends checking network status before attempting to fetch ads so the app does not continuously try to fetch when offline. You can use ConnectivityManager as shown in the official Android docs: https://developer.android.com/training/basics/network-ops/managing.html
Original illustrative code from the post:
private static final String DEBUG_TAG = "NetworkStatusExample";
...
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean isWifiConn = networkInfo.isConnected();
networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
boolean isMobileConn = networkInfo.isConnected();
Log.d(DEBUG_TAG, "Wifi connected: " + isWifiConn);
Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);
The author provided a helper class NetworkUtil; you can download it here: https://sites.google.com/site/karjolblogs/downloads/NetworkUtil.java?attredirects=0&d=1
Use it like this (from the original post):
NetworkUtil net = new NetworkUtil();
int status = net.getConnectivityStatus(getApplicationContext());
if (status != 0) {
// Code if the network connection is there
ad.loadAd(new AdRequest.Builder().build());
ad.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(TAG, "Ad Received");
// code when the ad loaded
}
@Override
public void onAdFailedToLoad(int errorcode) {
Log.d(TAG, "Ad Not Loaded Received");
// code if the ad loading is failed
}
});
} else {
// Code if the network connection is NOT there
}
Required permission
To check network status at runtime you must have the following permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
(Ensure your NetworkUtil implementation relies on this permission.)
Updated notes for newer Google Mobile Ads SDKs
The post originates from 2015; Google Mobile Ads SDK APIs have evolved. Two practical points to add for modern readers:
- Initialize the Mobile Ads SDK early in your app (for example in Application.onCreate()):
// Example initialization (SDK versions may vary)
MobileAds.initialize(context, initializationStatus -> { /* optional callback */ });
- In recent SDK versions, onAdFailedToLoad has a different signature and provides richer error information (for example via LoadAdError). Here is an example that shows both an updated listener and how to inspect the error for troubleshooting:
// Updated example (SDK may require different imports; check your SDK docs)
adView = findViewById(R.id.adView);
MobileAds.initialize(this, initializationStatus -> {});
AdRequest adRequest = new AdRequest.Builder().build();
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d(TAG, "Ad Received");
// show ad view or update UI
}
@Override
public void onAdFailedToLoad(LoadAdError adError) {
Log.d(TAG, "Ad Not Loaded Received: " + adError.toString());
// Inspect adError.getCode() or adError.getMessage() for debugging
// hide ad view or implement retry/backoff
}
});
adView.loadAd(adRequest);
Refer to the official Google Mobile Ads documentation for the SDK version you use (the original article linked the AdView docs: https://developers.google.com/android/reference/com/google/android/gms/ads/AdView).
Practical tips and recommended behavior
- Hide the ad container when no ad is available to avoid empty space in your UI.
- Implement exponential backoff for retries rather than continuous immediate retries.
- Log errors with enough detail to diagnose (error code/message) but avoid leaking sensitive user data.
- Ensure you comply with AdMob policies when changing ad behavior.
References and downloads
- AdView reference (original link preserved): https://developers.google.com/android/reference/com/google/android/gms/ads/AdView
- ConnectivityManager docs (original link preserved): http://developer.android.com/training/basics/network-ops/managing.html
- NetworkUtil.java (as provided by the original author): https://sites.google.com/site/karjolblogs/downloads/NetworkUtil.java?attredirects=0&d=1
Final notes
That’s it. Happy Coding..
No comments:
Post a Comment