when user click on button/cardview then they will see ad after showing ad i wanna do something like Toast or other

Viewed 61

i want to add something like this in android studio when user click on button/cardview then they will see ad after showing ad i wanna do something like Toast or other

here is my java code

adbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//before that i want to show ad and after showing ad i want to do this
                int i = Integer.parseInt(pointstext.getText().toString());
                pointstext.setText(String.valueOf(i + 150));
            }
        });

and XML

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="000000"
    android:id="@+id/points"
    android:layout_margin="8dp"
    android:textColor="@color/white"
    android:gravity="center"/>

1 Answers

Load an ad


To load an interstitial ad, call the InterstitialAd static load() method and pass in an InterstitialAdLoadCallback to receive the loaded ad or any possible errors. Notice that like other format load callbacks, InterstitialAdLoadCallback leverages LoadAdError to provide higher fidelity error details.

import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

public class MainActivity extends Activity {

    private InterstitialAd mInterstitialAd;

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

      MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
      });
      AdRequest adRequest = new AdRequest.Builder().build();

      InterstitialAd.load(this,"your-google-ad-key", adRequest,
        new InterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        // The mInterstitialAd reference will be null until
        // an ad is loaded.
        mInterstitialAd = interstitialAd;
        Log.i(TAG, "onAdLoaded");
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // Handle the error
        Log.i(TAG, loadAdError.getMessage());
        mInterstitialAd = null;
      }
    });
  }
}

Show the ad


In the onClick method of the button add this:

adbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mInterstitialAd != null) {
                    mInterstitialAd.show(MyActivity.this);
                } else {
                    Log.d("TAG", "The interstitial ad wasn't ready yet.");
                }
            }
        });

Know when ad finished


Add this code:

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdClosed() {
        //AdClosed now reward user with points etc
        int i = Integer.parseInt(pointstext.getText().toString());
        pointstext.setText(String.valueOf(i + 150));
    }
});
Related