AdMob Banner Interstitial Rewarded Video Ads Android Example – In this video i will share how to integrate Google AdMob ads in Banner, Interstitial, Rewarded Video Ads in android application example. Google AdMob is one of the best platform to earn more money via android application.
Through the AdMob we earn passive income in free time. For that you need strong knowledge in Java and Firebase or MySQL database to upload apps on play store. Most of android developers following this way to generate side income.
Therefore here I created one example article which is covered Google Admob banner ads, interstitial, native, Anchor and rewarded video advertisements. So this example surely helps to build your own android application and via the code you can easily setup the ads.
Everyone likes to make money online while uploading android application in google play store. But some students or developers are struggle to integrate AdMob into our Android Application. So here i have explain the full procedure to make Banner, Interstitial & Rewarded Video Ads example.
AdMob Banner Interstitial Rewarded Video Ads Android Example
Let’s start our tutorial to integrate admob banner, interstitial and rewarded video ads example. Create New project and give some name for your android application project & choose Empty Activity, after creating the project just open the default class of MainActivity.java and add the following code like below,
MainActivity.java
package in.vetbossel.googleadmob; import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.InterstitialAd; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.reward.RewardItem; import com.google.android.gms.ads.reward.RewardedVideoAd; import com.google.android.gms.ads.reward.RewardedVideoAdListener; public class MainActivity extends AppCompatActivity { Context context; private InterstitialAd mInterstitialAd; private RewardedVideoAd mRewardedVideoAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // Banner AdView mAdView = findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); // Interstitial mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); AdRequest adInterstitialRequest = new AdRequest.Builder().build(); mInterstitialAd.loadAd(adInterstitialRequest); //RewardedVideoAd mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build()); mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener); } public void showInterstitialAd(View view) { if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } } public void showRewardedAd(View view) { if (mRewardedVideoAd != null && mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); } } RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() { @Override public void onRewardedVideoAdLoaded() { Toast.makeText(context, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdOpened() { Toast.makeText(context, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoStarted() { Toast.makeText(context, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdClosed() { Toast.makeText(context, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); } @Override public void onRewarded(RewardItem rewardItem) { String msg = "onRewarded! currency: " + rewardItem.getType() + " amount: " + rewardItem.getAmount(); Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(context, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdFailedToLoad(int i) { Toast.makeText(context, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show(); } }; }
Hereafter we dont need to create java classes just move on XML files to integrate JAVA files. MainActivity.java files cover the all ads like banner ad, interstitial ad, amd rewarded video ads. In this example i will give google Test Unit Id and App Id for test the android application. You have change that after publishing your android application in google play store.
Activity Layouts
Now we need to intefrate java classes with XML file to visible the Google AdMob Ads in our android application. Open under the path of res =>layout =>activity_main.xml file and add the following below code in your project. Just change the Unit Id, already if you have Unit Id for your android application or just leave we have assume the Google Test Id to execute the ad.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="in.vetbossel.googleadmob.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txt_interstitial_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:background="@color/colorPrimaryDark" android:clickable="true" android:gravity="center" android:onClick="showInterstitialAd" android:padding="10dp" android:text="Show Interstitial Ads" android:textColor="@android:color/white" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/txt_interstitial_add" android:layout_margin="15dp" android:background="@color/colorPrimaryDark" android:clickable="true" android:gravity="center" android:onClick="showRewardedAd" android:padding="10dp" android:text="Show Rewarded Video Ads" android:textColor="@android:color/white" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="Google AdMob" android:textColor="@android:color/black" android:textSize="20sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111" /> </RelativeLayout> </android.support.constraint.ConstraintLayout>
Download Source Code
You can download the full source code here. Suppose if you face any error feel free to comment below i will help to how to fix the problem in integrate AdMob Ads android application example.