Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if AdView is visible?

I want to check if AdView did load ads and is visible and therefore requires space on the display or if did not load an ad for example if internet connection is not available. If it did not load ads I can use the space for something else.

How to accomplish this?

like image 875
MacMarde Avatar asked Mar 01 '14 07:03

MacMarde


People also ask

How do I know if a banner ad is loaded?

You can use the getResponseInfo() method to retrieving information about the loaded ad.

How to load native ads in android?

Load an Ad. Native ads are loaded via the AdLoader class, which has its own Builder class to customize it during creation. By adding listeners to the AdLoader while building it, an app specifies which types of native ads it is ready to receive. The AdLoader then requests just those types.


2 Answers

You may implement AdListener for this purpose. Just override onAdFailedToLoad and onAdLoaded.

like image 82
Denis Itskovich Avatar answered Sep 27 '22 22:09

Denis Itskovich


mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new AdListener() {
    // Called when an ad is loaded.
    @Override
    public void onAdLoaded() {
        Log.e(TAG, "Google onAdLoaded");
    }

    // Called when an ad failed to load.
    @Override
    public void onAdFailedToLoad(int error) {
        String message = "Google onAdFailedToLoad: " + getErrorReason(error);
        Log.e(TAG, message);
    }

    // Called when an Activity is created in front of the app
    // (e.g. an interstitial is shown, or an ad is clicked and launches a new Activity).
    @Override
    public void onAdOpened() {
        Log.e(TAG, "Google onAdOpened");
    }

    // Called when an ad is clicked and about to return to the application.
    @Override
    public void onAdClosed() {
        Log.e(TAG, "Google onAdClosed");
    }

    // Called when an ad is clicked and going to start a new Activity that will leave the application
    // (e.g. breaking out to the Browser or Maps application).
    @Override
    public void onAdLeftApplication() {
        Log.d(TAG, "Google onAdLeftApplication");
    }
});
mAdRequest = new AdRequest.Builder().build();
mAdView.loadAd(mAdRequest);


private String getErrorReason(int errorCode) {
    // Gets a string error reason from an error code.
    String errorReason = "";
    switch (errorCode) {
        case AdRequest.ERROR_CODE_INTERNAL_ERROR:
            errorReason = "Internal error";
            break;
        case AdRequest.ERROR_CODE_INVALID_REQUEST:
            errorReason = "Invalid request";
            break;
        case AdRequest.ERROR_CODE_NETWORK_ERROR:
            errorReason = "Network Error";
            break;
        case AdRequest.ERROR_CODE_NO_FILL:
            errorReason = "No fill";
            break;
    }
    return errorReason;
}
like image 42
Philip Herbert Avatar answered Sep 27 '22 20:09

Philip Herbert