Show the button only when the interstitial Ad is loaded, else show a TextView?

admob interstitial ads policy
show interstitial ad
banner and interstitial ads
the interstitial wasn t loaded yet
interstitial ads not showing android
admob interstitial ad unit id
onadclosed admob
admob error_code_internal_error

I'm trying to show a button to that displays the AdMob interstitial ad and takes the user to the next activity when closed. But I want the Button to be visible when the interstitial ad is loaded, till then I want to display a TextView that says that "the app is loading...".

This is what I have created so far.

Button goFree;
private TextView loadingFree;
private InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ask);
    Button goFree = (Button) findViewById(R.id.goFree);
    loadingFree = findViewById(R.id.loadingFeee);

    MobileAds.initialize(RegisterNote.this, "cca-app-pub-3940256099942544/1033173712");
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            startActivity(new Intent(Ask.this, Free.class));
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

    if (mInterstitialAd.isLoaded()) {
        goFree.setVisibility(View.VISIBLE);
        lodingFree.setVisibility(View.GONE);
    } else {
        goFree.setVisibility(View.GONE);
        lodingFree.setVisibility(View.VISIBLE);

    }

    goFree.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mInterstitialAd.show();
        }
    });
}

But the problem is even when the ad loads, the TextView visibility stays VISIBLE and button stays GONE. Maybe because when the ad loads, it doesn't call the function to change the visibility.

What should I do here? How can show the button only after the ad is loaded?

Try using onAdLoaded()function like you did with onAdClosed(), the code should be similar to this :

     goFree.setVisibility(View.GONE);
     lodingFree.setVisibility(View.VISIBLE);

     mInterstitialAd.setAdListener(new AdListener() {
                 @Override
                 public void onAdLoaded() {
                     goFree.setVisibility(View.VISIBLE);
                     lodingFree.setVisibility(View.GONE);
                 }

                 @Override
                 public void onAdClosed() {
                     startActivity(new Intent(Ask.this, Free.class));
                     mInterstitialAd.loadAd(new AdRequest.Builder().build());
                 }
             });

And you can remove the check if else after.

Source : https://developers.google.com/android/reference/com/google/android/gms/ads/InterstitialAd

Hope this helps

Interstitial Ads | Android, When an app shows an interstitial ad, the user has the choice to either interstitial ads over the course of an activity's lifespan, so you only need to construct it once. The easiest way to load test ads is to use our dedicated test ad unit code example could be shown in a button's OnClickListener like this:. Hello I am having a problem with interstitial ads. I have set a text to show me which function has been executed and it shows that the ad is loaded. But when I press the button to show the ad it says "showing ad" on the text but nothing appears on the screen. @rampara I know you are the guru here so I'm hoping you could help :D.

Each of the overridable methods in AdListener corresponds to an event in the lifecycle of an ad.

You can write your code in onAdLoaded() method when the add is loaded it will hide textview and visible button.

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.
        goFree.setVisibility(View.VISIBLE);
        lodingFree.setVisibility(View.GONE);
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an ad request fails.
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when the ad is displayed.
    }

    @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 interstitial ad is closed.
    }

InterstitialAd | Google APIs for Android, If the button is clicked before the interstitial is loaded, the user should proceed to // the isLoaded()) { mInterstitialAd.show(); } else { // Proceed to the next level. addView(mNextLevelButton); // Create a TextView to display the current level. googleads-mobile-android. Contribute to googleads/googleads-mobile-android-examples development by creating an account on GitHub.

Check the below example that i have implemented. Hope it will help you. Just check this method

    holder.cv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                  //  showInterstitial();

                    if (interstitialAd != null && interstitialAd.isLoaded()) {
                        interstitialAd.show();
                    } else {
                        Intent intent = new Intent();
                        Bundle bundle = 
     ActivityOptions.makeSceneTransitionAnimation((Activity) mContext, 
     holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
                        intent.setClass(mContext, AboutMovieActivity.class);
                        intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID, 
   mSearchedMovies.get(position).getId());
                        intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH, 
   mSearchedMovies.get(position).getPosterPath());
                        intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME, 
    mSearchedMovies.get(position).getTitle());
                        mContext.startActivity(intent, bundle);
                        startGame();
                    }

                    interstitialAd.setAdListener(new AdListener(){

                        @Override
                        public void onAdClosed() {
                            Intent intent = new Intent();
                            Bundle bundle = 
     ActivityOptions.makeSceneTransitionAnimation((Activity) mContext, 
     holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
                            intent.setClass(mContext, AboutMovieActivity.class);
                            intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID, 
     mSearchedMovies.get(position).getId());
                            intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH, 
    mSearchedMovies.get(position).getPosterPath());
                            intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME, 
    mSearchedMovies.get(position).getTitle());
                            mContext.startActivity(intent, bundle);
                        }
                    });




    Full code is below:-

     public class MoviesSearchAdapter extends 
    RecyclerView.Adapter<MoviesSearchAdapter.ViewHolder> {
    Context mContext;
    private ArrayList<Movie> mSearchedMovies;
    private InterstitialAd interstitialAd;

    public MoviesSearchAdapter(ArrayList<Movie> searchedMovies, Context context) {
        mContext = context;
        mSearchedMovies = searchedMovies;
      }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = 
    LayoutInflater.from(parent.getContext()).inflate
     (R.layout.search_movie_list_item, 
    parent, false);

        // Initialize the Mobile Ads SDK.   ca-app-pub-7152815429504851~7360705844
        MobileAds.initialize(mContext, "ca-app-pub-7152815429504851~8085885141");

        // Create the InterstitialAd and set the adUnitId.
        interstitialAd = new InterstitialAd(mContext);
        // Defined in res/values/strings.xml  ca-app-pub- 
      7152815429504851/2822695030
        interstitialAd.setAdUnitId("ca-app-pub-7152815429504851/4913761040");

        startGame();

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (mSearchedMovies != null) {
            holder.name.setText(mSearchedMovies.get(position).getTitle());
            Picasso.with(mContext).load(URLConstants.IMAGE_BASE_URL + 
     mSearchedMovies.get(position).getPosterPath()).into(holder.thumbnailImage);
            if (mSearchedMovies.get(position).getDate().length() >= 5) {
                String date = mSearchedMovies.get(position).getDate().substring(0, 
      4);
                holder.releaseDate.setText(date);
            }
            String rating = 
     Double.toString(mSearchedMovies.get(position).getRating());
            holder.rating.setText(rating);
            holder.cv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                  //  showInterstitial();

                    if (interstitialAd != null && interstitialAd.isLoaded()) {
                        interstitialAd.show();
                    } else {
                        Intent intent = new Intent();
                        Bundle bundle = 
     ActivityOptions.makeSceneTransitionAnimation((Activity) mContext, 
    holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
                        intent.setClass(mContext, AboutMovieActivity.class);
                        intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID, 
    mSearchedMovies.get(position).getId());
                        intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH, 
    mSearchedMovies.get(position).getPosterPath());
                        intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME, 
    mSearchedMovies.get(position).getTitle());
                        mContext.startActivity(intent, bundle);
                        startGame();
                    }

                    interstitialAd.setAdListener(new AdListener(){

                        @Override
                        public void onAdClosed() {
                            Intent intent = new Intent();
                            Bundle bundle = 
     ActivityOptions.makeSceneTransitionAnimation((Activity) mContext, 
    holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
                            intent.setClass(mContext, AboutMovieActivity.class);
                            intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID, 
    mSearchedMovies.get(position).getId());
                            intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH, 
    mSearchedMovies.get(position).getPosterPath());
                            intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME, 
    mSearchedMovies.get(position).getTitle());
                            mContext.startActivity(intent, bundle);
                        }
                    });



                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mSearchedMovies.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        ImageView thumbnailImage;
        TextView name;
        TextView releaseDate;
        TextView rating;

        public ViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cardView);
            thumbnailImage = (ImageView) 
     itemView.findViewById(R.id.thumbnailImageView);
            name = (TextView) itemView.findViewById(R.id.nameTextView);
            releaseDate = (TextView) 
     itemView.findViewById(R.id.releaseDateTextView);
            rating = (TextView) itemView.findViewById(R.id.ratingTextView);
        }
    }

    private void showInterstitial() {
        // Show the ad if it's ready. Otherwise toast and restart the game.
        if (interstitialAd != null && interstitialAd.isLoaded()) {
            interstitialAd.show();
        } else {
            //Toast.makeText(this, "Your Internet is not Working", 
     Toast.LENGTH_SHORT).show();
            startGame();
        }
    }

    private void startGame() {
        // Request a new ad if one isn't already loaded, hide the button, and kick 
     off the timer.
        if (!interstitialAd.isLoading() && !interstitialAd.isLoaded()) {
            AdRequest adRequest = new AdRequest.Builder().build();
            interstitialAd.loadAd(adRequest);
        }

        //  retryButton.setVisibility(View.INVISIBLE);

      }
     }

How to Add AdMob Interstitial Ads in Your Android Apps, The following example will show you how to add interstitial ads in your android apps. When user click the load interstitial button, the example app will send the ad request to the adMob isReady()) { interstitialAds.show(); } else { textView. 1. when i use your code, I got only NDTV image on emulator. 2. The following code show the banner ad at the bottom of the application and show interstitial ad when the button is clicked. No problem with banner ad. The problem is, when the app start and click button for the first time, the interstitial is shown but the interstitial is not shown again starting from second time the button is clicked.

Hello Hijibiji try this code it will help you.

    mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        // Code to be executed when an ad finishes loading.
        goFree.setVisibility(View.VISIBLE);
        lodingFree.setVisibility(View.GONE);
    }

    @Override
    public void onAdFailedToLoad(int errorCode) {
        // Code to be executed when an ad request fails.
    }

    @Override
    public void onAdOpened() {
        // Code to be executed when the ad is displayed.
    }

    @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 interstitial ad is closed.
    }
});

Refer this link

android-ads/MyActivity.java at master · googlearchive/android-ads , Contribute to googlearchive/android-ads development by creating an account on TextView;. import android.widget.Toast;. import com.google.android.gms.ads. Create the "retry" button, which tries to show an interstitial between game plays​. interstitialAd.show();. } else {. Toast.makeText(this, "Ad did not load", Toast. Android is one of the most popular operating systems for mobile. In this article, I will show you how to show interstitial ads every 20 seconds in an Android application using Android studio.

Admob Interstitial Ads on Multiple Button Click, isLoaded()){ interstitialAd.show(); } else { startActivity(new the reward video loading takes much time to load and second the reward video sometimes is requested only once so Button btn=(Button)findViewById(R.id.btncomplete); TextView  Interstitial ads should be displayed during natural pauses in the flow of an app. Between levels of a game is a good example, or after the user completes a task. To show an interstitial, use the isLoaded () method to verify that it's done loading, then call show () . The interstitial ad from the previous code example could be shown in a button

Help : Admob Interstitial ads shows up only …, On closing the ad, the counter gets added to one, it gets dis… Admob Interstitial ads shows up only once in the App by brvnbld TextView; import com.​google.android.gms.ads. isLoaded()) { mInterstitialAd.show(); } else { mInterstitialAd. You need to load new ad each time user clicks on the button. In this simple example, the press of a // button is used instead. // // If the button is clicked before the interstitial is loaded, the user should proceed to // the next part of the app (in this case, the next level).

Delay and problem with interstitial ads, Now touch your "Retry" button, wait for the interstitial, close it, and see The switch button will not move anymore; During the count down, your textview does not display the entire text I only modified activity_my.xml and MyActivity.java 2) load interstitial - interstitial is loaded when onActivityResult is  Step 4: Showing the Ad. Finally, after the interstitial ad is loaded, you can call the Show method to render the interstitial ad on the screen. For example you can have a function for ShowInterstitial, and call this function when it's time to show the interstitial ad:

Comments
  • Restructure it a little: put goFree.setVisibility(View.INVISIBLE); and lodingFree.setVisibility(View.GONE); in your onCreate method and only change it in the isLoaded() method
  • Wow! This is perfect. Well articulated and working just the way I wanted it to be. Thanks for the effort. Cheers.