Home » Android » android – Loading Ad (adMob) on Background Thread

android – Loading Ad (adMob) on Background Thread

Posted by: admin June 15, 2020 Leave a comment


I want to load my add on a background thread cause it makes the SlidingMenu laggy upon opening and closing. Should I use a Thread/Handler? Or AsyncTask?

String MY_AD_UNIT_ID = "----";
AdView adView = new AdView(getActivity(), AdSize.BANNER, MY_AD_UNIT_ID);
final LinearLayout adLayout = (LinearLayout) getActivity()
adView.loadAd(new AdRequest());
How to&Answers:

I do not believe this can be done as all UI related stuff has to be done on the main thread. The API probably already has a thread to gets the ad on the network. If it didnt android would throw a NetworkOnMainThreadException if any network related stuff is done on the main thread


This can be achieved by loading the Ad on UI Thread by runOnUiThread

Call this from onCreate()

    Thread adThread = new Thread()
        public void run()

loadAd() method

private void loadAd()
    // Banner Ad
    final AdView adview = (AdView) this.findViewById(R.id.adview);

    // Request for ads
    final AdRequest adRequest = new AdRequest.Builder()
            // Time for test devices

    // Load Ads on UI Thread
    runOnUiThread(new Runnable()
        public void run()


You should use MobileAds.initialize() method before ads loading. After that loadAd works faster