Search code examples
javaandroidandroid-recyclerviewprogressdialoginterstitial

Unable to show progress dialog SPECIFICALLY when opening interstitial ad from recyclerview


I want to add interstitial ad between two activities. My first activity is recyclerView. When I click on it I open another activity.

I have two working solutions, but neither gives me the performance I want. Issue in both cases is that the ad takes time to load.

  • First solution - I open the second activity and then load interstitial. Issue here is that user opens activity, and has a few seconds to click around before the ad interrupts. Ideally activity would not be loaded before the ad is shown. This breaks the flow.
  • Second solution (This is my preferred solution) - I click on recyclerview and it opens interstitial. When I click on the close ad, then it opens the new activity. Issue here is that when the user clicks on recyclerview item, it takes time to open interstitial, it can be confusing to user, and the user might start clicking multiple times.

My ideal solution, based on my second solution, would be this: user clicks on recyclerview item, loader appears, interstitial add is shown. I have added a loader in my onClick, and it appears when I open the item directly, but it doesn't when I open the interstitial.

This is my current code based on my second solution:

//region Item touch listener
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), listView, new itemsFragment.ClickListener() {
    @Override
    public void onClick(View view, int position) {
        Item item = itemList.get(position);
        String itemId = item.getId();

    //STARTING LOADER HERE
        ProgressDialog progress = new ProgressDialog(this);
        progress.setTitle("Loading");
        progress.setCancelable(false); 
        progress.show();

    //OPEN FOR CONDITION
        if ((counter % 2) == 0) {
        //LOADER NOT VISIBLE 
            loadInterstitialAd(itemId);
        } else {
        //LOADER VISIBLE
            fetchItem(itemId);
        }

    //CLOSING LOADER HERE
        progress.dismiss();
    }
}));
//endregion

...

//region Interstitial ad
private void loadInterstitialAd(String itemId) {
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(SAMPLE_INTERSTITIAL_ID);
    mInterstitialAd.setAdListener(new AdListener() {

        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            Log.e(TAG, "Loaded interstitial ad");
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }
        }

        @Override
        public void onAdFailedToLoad(int i) {
            super.onAdFailedToLoad(i);
            Log.e(TAG, "Failed to load interstitial ad");
        }

        @Override
        public void onAdClosed() {
            super.onAdClosed();
            fetchItem(itemId);
        }
    });

    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
}
//endregion

Solution

  • I got this to work based on Selvin's comment, by moving the show progress to loadInterstitial ad, and hiding it before the ad opens.