Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
To integrate Google AdMob Rewarded Video Ads in an Android application, follow the steps below:
In your build.gradle
(app module), add the Google Mobile Ads SDK:
implementation 'com.google.android.gms:play-services-ads:latest_version'
Replace latest_version
with the most recent version of the SDK. Then, sync the project.
In your AndroidManifest.xml
, add the following meta-data tag inside the <application>
tag:
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="YOUR_ADMOB_APP_ID"/>
Replace YOUR_ADMOB_APP_ID
with your AdMob App ID.
Initialize the SDK in the onCreate()
method of your Application
class:
import com.google.android.gms.ads.MobileAds; public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); MobileAds.initialize(this, initializationStatus -> {}); } }
Make sure you reference MyApplication
in your AndroidManifest.xml
:
<application android:name=".MyApplication" ...> </application>
In your activity or fragment:
import com.google.android.gms.ads.rewarded.RewardedAd; import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback; public class YourActivity extends AppCompatActivity { private RewardedAd rewardedAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout); rewardedAd = new RewardedAd(this, "YOUR_AD_UNIT_ID"); RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() { @Override public void onRewardedAdLoaded() { // Ad successfully loaded, can now be shown if (rewardedAd.isLoaded()) { rewardedAd.show(YourActivity.this, rewardItem -> { // User earned the reward }); } } @Override public void onRewardedAdFailedToLoad(int errorCode) { // Ad failed to load } }; rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback); } }
Replace YOUR_AD_UNIT_ID
with your AdMob Rewarded Video Ad Unit ID.
Before releasing, ensure you test your integration. Use Google's test ad units so you don't accidentally click on your own live ads, which can result in your AdMob account being suspended:
rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
This is a test Ad Unit ID for Rewarded Videos from Google. Make sure to replace it with your actual Ad Unit ID before releasing your app.
After integrating, be sure to handle all the lifecycle and reward callbacks as necessary for your app's use case. Once testing is complete, ensure you've replaced any test Ad Unit IDs with real ones.
Remember to always keep user experience in mind. Ensure your users understand why and when they'll see rewarded videos and what they'll get in return.
Adding AdMob rewarded video ads to Android Studio project:
build.gradle
file and add the AdMob SDK dependency.implementation 'com.google.android.gms:play-services-ads:20.4.0'
Displaying AdMob rewarded videos in Android app:
// MainActivity.java import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.google.android.gms.ads.AdError; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.rewarded.RewardItem; import com.google.android.gms.ads.rewarded.RewardedAd; import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback; public class MainActivity extends AppCompatActivity { private RewardedAd rewardedAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the Mobile Ads SDK MobileAds.initialize(this); // Load the rewarded video ad loadRewardedAd(); // Show the rewarded video ad when a button is clicked findViewById(R.id.showRewardedButton).setOnClickListener(view -> showRewardedAd()); } private void loadRewardedAd() { RewardedAd.load(this, getString(R.string.admob_rewarded_ad_unit_id), new AdRequest.Builder().build(), new RewardedAdLoadCallback() { @Override public void onAdLoaded(RewardedAd ad) { rewardedAd = ad; } @Override public void onAdFailedToLoad(AdError adError) { // Handle rewarded video ad loading failure } }); } private void showRewardedAd() { if (rewardedAd != null) { rewardedAd.show(this, rewardItem -> { // User completed watching the rewarded video ad // Grant rewards to the user }); } else { // Rewarded video ad not loaded, handle accordingly loadRewardedAd(); // Retry loading the ad or show an alternative } } }
Make sure to replace "@string/admob_rewarded_ad_unit_id"
with your actual AdMob rewarded video ad unit ID.