1 ) Activity Main
2 ) Main Activity
3 ) My Application
Notes :
1 ) Require Gradle
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" xmlns:ads="http://schemas.android.com/apk/res-auto" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/msg_welcome" /> <Button android:id="@+id/btn_fullscreen_ad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/btn_fullscreen_ad" /> <Button android:id="@+id/btn_show_rewarded_video" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_fullscreen_ad" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="@string/btn_rewarded_video" /> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="@string/banner_home_footer"/> </RelativeLayout>
2 ) Main Activity
public class MainActivity extends AppCompatActivity { private AdView mAdView; private Button btnFullscreenAd, btnShowRewardedVideoAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnFullscreenAd = (Button) findViewById(R.id.btn_fullscreen_ad); btnShowRewardedVideoAd = (Button) findViewById(R.id.btn_show_rewarded_video); btnFullscreenAd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, InterstitialAdActivity.class)); } }); btnShowRewardedVideoAd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, RewardedVideoAdActivity.class)); } }); // TODO - remove this if condition // it's for demo purpose if (TextUtils.isEmpty(getString(R.string.banner_home_footer))) { Toast.makeText(getApplicationContext(), "Please mention your Banner Ad ID in strings.xml", Toast.LENGTH_LONG).show(); return; } mAdView = (AdView) findViewById(R.id.adView); // mAdView.setAdSize(AdSize.FULL_BANNER); // mAdView.setAdUnitId(getString(R.string.banner_home_footer)); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // Check the LogCat to get your test device ID .addTestDevice("C04B1BFFB0774708339BC273F8A43708") .build(); mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { } @Override public void onAdClosed() { Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show(); } @Override public void onAdFailedToLoad(int errorCode) { Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show(); } @Override public void onAdLeftApplication() { Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show(); } @Override public void onAdOpened() { super.onAdOpened(); } }); mAdView.loadAd(adRequest); } @Override public void onPause() { if (mAdView != null) { mAdView.pause(); } super.onPause(); } @Override public void onResume() { super.onResume(); if (mAdView != null) { mAdView.resume(); } } @Override public void onDestroy() { if (mAdView != null) { mAdView.destroy(); } super.onDestroy(); } }
3 ) My Application
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // initialize the AdMob app MobileAds.initialize(this, getString(R.string.admob_app_id)); } }
4) InterstitialAdActivity
public class InterstitialAdActivity extends AppCompatActivity {private String TAG = InterstitialAdActivity.class.getSimpleName(); InterstitialAd mInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_interstitial_ad); mInterstitialAd = new InterstitialAd(this); // set the ad unit ID mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen)); AdRequest adRequest = new AdRequest.Builder() .build(); // Load ads into Interstitial Ads mInterstitialAd.loadAd(adRequest); mInterstitialAd.setAdListener(new AdListener() { public void onAdLoaded() { showInterstitial(); } }); } private void showInterstitial() { if (mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } } }
5 ) RewardedVideoAdActivity
public class RewardedVideoAdActivity extends AppCompatActivity {
6 ) String XMLprivate RewardedVideoAd mRewardedVideoAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rewarded_video_ad); MobileAds.initialize(this, getString(R.string.admob_app_id)); mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() { @Override public void onRewarded(RewardItem rewardItem) { Toast.makeText(RewardedVideoAdActivity.this, "onRewarded! currency: " + rewardItem.getType() + " amount: " + rewardItem.getAmount(), Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdClosed() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdFailedToLoad(int errorCode) { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoCompleted() { } @Override public void onRewardedVideoAdLoaded() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdOpened() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoStarted() { Toast.makeText(RewardedVideoAdActivity.this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); } }); loadRewardedVideoAd(); } private void loadRewardedVideoAd() { mRewardedVideoAd.loadAd(getString(R.string.rewarded_video), new AdRequest.Builder().build()); // showing the ad to user showRewardedVideo(); } private void showRewardedVideo() { // make sure the ad is loaded completely before showing it if (mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); } } @Override public void onResume() { mRewardedVideoAd.resume(this); super.onResume(); } @Override public void onPause() { mRewardedVideoAd.pause(this); super.onPause(); } @Override public void onDestroy() { mRewardedVideoAd.destroy(this); super.onDestroy(); } }
<resources> <string name="app_name">GoogleAdDemo</string> <string name="title_activity_second_activiy">Interstitial</string> <string name="msg_welcome">Welcome to Admob. Click on the below button to launch the Interstitial ad.</string> <string name="btn_fullscreen_ad">Show Interstitial Ad</string> <string name="btn_rewarded_video">Show Rewarded Video Ad</string> <string name="admob_app_id">ca-app-pub-5143428618938264~9382068217</string> <string name="banner_home_footer">ca-app-pub-5143428618938264/2788739701</string> <string name="interstitial_full_screen">ca-app-pub-5143428618938264/3790830187</string> <string name="rewarded_video">ca-app-pub-3940256099942544/5224354917</string> </resources>
Notes :
1 ) Require Gradle
implementation 'com.android.support:appcompat-v7:27.1.1'implementation 'com.android.support.constraint:constraint-layout:1.1.1'androidTestImplementation 'com.android.support.test:runner:1.0.2'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'implementation 'com.google.android.gms:play-services-ads:15.0.1'
Comments
Post a Comment