How to create rewarded video ads with dialog?

0 Comments


In this article, we create a dialog popup in the app and when the dialog is open and if you click the OK button then our rewarded video ads show and when we close video ads after completion of time then directly open our main activity and also when press back in the main activity then never back dialog again.

So we have some steps, you can follow stepwise. Definitely you are able to create this type of ads project.

Step: 1

First, we create a new project and if you have an existing project whatever you created then you can start to implement code in your project.

Step: 2

Now you can implement SDK integration which means dependency on your project and goes to build.gradle(Module: app) under Gradle Script Files. The code is given below.

implementation 'com.google.android.gms:play-services-ads:19.1.0'

Step: 3

After that, you can integrate the APP ID in your Manifest.xml file under the manifest code given below.

<meta-data
   android:name="com.google.android.gms.ads.APPLICATION_ID"
   android:value="ca-app-pub-3940256099942544~3347511713" />

Step: 4

Now you can give permission for the INTERNET then you are able to show ads in your android apps.

<uses-permission android:name="android.permission.INTERNET" />

Step: 5

Now you can import rewarded video ads unit ID and APP ID in your string.xml file. So go to res folder and click values folder and open string.xml file then you can paste below code under resource directories.

<string name="APP_ID">ca-app-pub-3940256099942544~3347511713</string>
<string name="ADMOB_UNIT_ID">ca-app-pub-3940256099942544/5224354917</string>

Step: 6

Now first we extend the Rewarded video Listener code given below.

implements RewardedVideoAdListener

Step: 7 

 After that, you can import variable code in your project given below.

RewardedVideoAd rewardedVideoAd;
AlertDialog dialog;

Step: 8

Now we create a dialog with java code in this article and we suggest you can create a dialog with JAVA because it is very safe and secure in this platform. This code you can place under the onCreate method. Do not paste out of the onCreate method in your project.

// Show Dialog Code
dialog = new AlertDialog.Builder(this)
       .setTitle("Title")
       .setMessage("Example Message")
       .setPositiveButton("Ok", null)
       .setNegativeButton("Cancel", null)
       .setCancelable(false)
       .show();
Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
       if (rewardedVideoAd.isLoaded()) {
           rewardedVideoAd.show();
       }
   }
});
Button negativeButton = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);
negativeButton.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
       MainActivity.super.onBackPressed();
   }
});

Step: 8

Finally, we import Rewarded video ads code in your project so do not miss any code in this article otherwise your app will be crashed. This code you can place under the onCreate method. Do not paste out of the onCreate method in your project.

// Rewarded Video Ads Codes
MobileAds.initialize(this, getString(R.string.APP_ID));
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
rewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();

Now, we import all rewarded video ads code onPause, onDestroy, onResume. Everything we create so copy all code given below and paste in your project under MainActivity extends AppCompactActivity. Do not paste under the onCreate method otherwise your project definitely crashes.

public void loadRewardedVideoAd() {
   rewardedVideoAd.loadAd(getString(R.string.ADMOB_UNIT_ID),
           new AdRequest.Builder().build());
}

@Override
public void onRewardedVideoAdLoaded() {
}

@Override
public void onRewardedVideoAdOpened() {
}

@Override
public void onRewardedVideoStarted() {
}

@Override
public void onRewardedVideoAdClosed() {
   loadRewardedVideoAd();
   dialog.dismiss();
}

@Override
public void onRewarded(RewardItem rewardItem) {
}

@Override
public void onRewardedVideoAdLeftApplication() {
}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}

@Override
public void onRewardedVideoCompleted() {
}

@Override
protected void onResume() {
   rewardedVideoAd.resume(this);
   super.onResume();
}

@Override
protected void onPause() {
   rewardedVideoAd.pause(this);
   super.onPause();
}

@Override
protected void onDestroy() {
   rewardedVideoAd.destroy(this);
   super.onDestroy();
}

Step: 9

Finally, the last thing import left is called onBackPressed function. This code is placed below the onDestroy method.

// On Back press Configuration
@Override
public void onBackPressed() {
   super.onBackPressed();
}

share this

Leave a Reply

Your email address will not be published. Required fields are marked *