Admob onAdLoaded was Called But nothing is shown in screen Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Admob onAdLoaded was Called But nothing is shown in screen without wasting too much if your time.

The question is published on by Tutorial Guruji team.

i was trying to use Admob using sample test id they have given. I’ve added maven repository in project level build.gradle and implemented play services ads in app build.gradle. when i run the app. onAdLoaded method was called. but there is nothing on my screen. i have tried with my own unique ad id too. but the result is same. please help

Main Activity

 package com.twotwotwo.dip.admobtry;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;

    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdSize;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.MobileAds;

    public class MainActivity extends AppCompatActivity {
        AdView mAdView;

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

            MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

            mAdView = new AdView(this);
            mAdView.setAdSize(AdSize.BANNER);
            mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

            AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
            mAdView.loadAd(adRequest);

            mAdView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    // Code to be executed when an ad finishes loading.
                    showToast("Ad loaded");
                    if (mAdView.getVisibility() == View.GONE) {
                        mAdView.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onAdFailedToLoad(int errorCode) {
                    // Code to be executed when an ad request fails.
                    if(errorCode == AdRequest.ERROR_CODE_INTERNAL_ERROR){
                        showToast(" Something happened internally");
                    } else if(errorCode == AdRequest.ERROR_CODE_INVALID_REQUEST){
                        showToast("The ad request was invalid; for instance, the ad unit ID was incorrect.");
                    } else if(errorCode == AdRequest.ERROR_CODE_NETWORK_ERROR){
                        showToast("The ad request was unsuccessful due to network connectivity.");
                    } else if(errorCode == AdRequest.ERROR_CODE_NO_FILL){
                        showToast("The ad request was successful, but no ad was returned due to lack of ad inventory.");
                    }
                }

                @Override
                public void onAdOpened() {
                    // Code to be executed when an ad opens an overlay that
                    // covers the screen.
                }

                @Override
                public void onAdLeftApplication() {
                    // Code to be executed when the user has left the app.
                }

                @Override
                public void onAdClosed() {
                    // Code to be executed when when the user is about to return
                    // to the app after tapping on an ad.
                }
            });
        }

        private void showToast(String message){
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
        }
    }

Answer

you are missing this line

mAdView=(AdView) findViewbyid(R.id.adView)
We are here to answer your question about Admob onAdLoaded was Called But nothing is shown in screen - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji