├── .gitignore ├── .idea ├── encodings.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── README.md ├── android_admob_tutorial_screenshot.jpg ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── shaheershukur │ │ └── admobtutorial │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── shaheershukur │ │ │ └── admobtutorial │ │ │ ├── BannerActivity.java │ │ │ ├── InterstitialActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── NativeActivity.java │ │ │ └── RewardedActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── demo_screen.png │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_banner.xml │ │ ├── activity_interstitial.xml │ │ ├── activity_main.xml │ │ ├── activity_native.xml │ │ ├── activity_rewarded.xml │ │ └── native_ad_layout.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── shaheershukur │ └── admobtutorial │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Admob Tutorial for Android : Reference Application 2 | A reference application for the [youtube video tutorials](https://www.youtube.com/playlist?list=PLXKzVP4cRi8AxXv2DoJcehtrhTxpNEQPM) on monetizing android applications using Google Admob. 3 | 4 | ![Screenshot](https://raw.githubusercontent.com/shaheershukur/AdmobTutorialForAndroid/master/android_admob_tutorial_screenshot.jpg) 5 | -------------------------------------------------------------------------------- /android_admob_tutorial_screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammed-shaheer-keerirakath/AdmobTutorialForAndroid/07a98fe2343e718d68feed28ca6bbb391ccff5f3/android_admob_tutorial_screenshot.jpg -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | buildToolsVersion "29.0.1" 6 | defaultConfig { 7 | applicationId "com.shaheershukur.admobtutorial" 8 | minSdkVersion 16 9 | targetSdkVersion 29 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | implementation 'androidx.appcompat:appcompat:1.0.2' 25 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 26 | implementation 'com.google.android.gms:play-services-ads:18.1.1' 27 | 28 | testImplementation 'junit:junit:4.12' 29 | androidTestImplementation 'androidx.test:runner:1.2.0' 30 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/shaheershukur/admobtutorial/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.shaheershukur.admobtutorial; 2 | 3 | import android.content.Context; 4 | 5 | import androidx.test.InstrumentationRegistry; 6 | import androidx.test.runner.AndroidJUnit4; 7 | 8 | import org.junit.Test; 9 | import org.junit.runner.RunWith; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getTargetContext(); 24 | 25 | assertEquals("com.shaheershukur.admobtutorial", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/shaheershukur/admobtutorial/BannerActivity.java: -------------------------------------------------------------------------------- 1 | package com.shaheershukur.admobtutorial; 2 | 3 | import android.os.Bundle; 4 | 5 | import androidx.appcompat.app.AppCompatActivity; 6 | 7 | import com.google.android.gms.ads.AdListener; 8 | import com.google.android.gms.ads.AdRequest; 9 | import com.google.android.gms.ads.AdView; 10 | import com.google.android.gms.ads.MobileAds; 11 | 12 | public class BannerActivity extends AppCompatActivity { 13 | 14 | private AdView bannerAdView; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_banner); 20 | 21 | MobileAds.initialize(this); 22 | bannerAdView = findViewById(R.id.banner_ad); 23 | 24 | AdRequest adRequest = new AdRequest.Builder().build(); 25 | bannerAdView.loadAd(adRequest); 26 | bannerAdView.setAdListener(new AdListener(){ 27 | @Override 28 | public void onAdClosed() { 29 | super.onAdClosed(); 30 | //do something on ad is closed 31 | } 32 | 33 | @Override 34 | public void onAdFailedToLoad(int i) { 35 | super.onAdFailedToLoad(i); 36 | //do something if ad failed to load 37 | } 38 | 39 | @Override 40 | public void onAdLeftApplication() { 41 | super.onAdLeftApplication(); 42 | } 43 | 44 | @Override 45 | public void onAdOpened() { 46 | super.onAdOpened(); 47 | } 48 | 49 | @Override 50 | public void onAdLoaded() { 51 | super.onAdLoaded(); 52 | } 53 | 54 | @Override 55 | public void onAdClicked() { 56 | super.onAdClicked(); 57 | } 58 | 59 | @Override 60 | public void onAdImpression() { 61 | super.onAdImpression(); 62 | } 63 | }); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/shaheershukur/admobtutorial/InterstitialActivity.java: -------------------------------------------------------------------------------- 1 | package com.shaheershukur.admobtutorial; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | import android.widget.TextView; 6 | 7 | import androidx.appcompat.app.AppCompatActivity; 8 | 9 | import com.google.android.gms.ads.AdListener; 10 | import com.google.android.gms.ads.AdRequest; 11 | import com.google.android.gms.ads.InterstitialAd; 12 | import com.google.android.gms.ads.MobileAds; 13 | 14 | public class InterstitialActivity extends AppCompatActivity { 15 | 16 | private InterstitialAd interstitialAd; 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_interstitial); 22 | 23 | MobileAds.initialize(this); 24 | interstitialAd = new InterstitialAd(this); 25 | interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); 26 | interstitialAd.loadAd(new AdRequest.Builder().build()); 27 | interstitialAd.setAdListener(new AdListener() { 28 | @Override 29 | public void onAdClosed() { 30 | super.onAdClosed(); 31 | interstitialAd.loadAd(new AdRequest.Builder().build()); 32 | } 33 | }); 34 | } 35 | 36 | public void nextLevel(View view) { 37 | if (interstitialAd.isLoaded()) { 38 | interstitialAd.show(); 39 | } 40 | 41 | TextView textView = findViewById(R.id.textView); 42 | textView.setText("Game Level 2"); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/shaheershukur/admobtutorial/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.shaheershukur.admobtutorial; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | 7 | import androidx.appcompat.app.AppCompatActivity; 8 | 9 | import com.google.android.gms.ads.MobileAds; 10 | import com.google.android.gms.ads.initialization.InitializationStatus; 11 | import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; 12 | 13 | public class MainActivity extends AppCompatActivity { 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_main); 19 | 20 | MobileAds.initialize(this); 21 | } 22 | 23 | public void openAdActivity(View view) { 24 | switch (view.getId()) { 25 | case R.id.banner_ad_button: 26 | Intent intentBanner = new Intent(this, BannerActivity.class); 27 | startActivity(intentBanner); 28 | break; 29 | case R.id.interstitial_ad_button: 30 | Intent intentInterstitial = new Intent(this, InterstitialActivity.class); 31 | startActivity(intentInterstitial); 32 | break; 33 | case R.id.native_ad_button: 34 | Intent intentNative = new Intent(this, NativeActivity.class); 35 | startActivity(intentNative); 36 | break; 37 | case R.id.rewarded_ad_button: 38 | Intent intentRewarded = new Intent(this, RewardedActivity.class); 39 | startActivity(intentRewarded); 40 | break; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/shaheershukur/admobtutorial/NativeActivity.java: -------------------------------------------------------------------------------- 1 | package com.shaheershukur.admobtutorial; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | import android.widget.Button; 6 | import android.widget.FrameLayout; 7 | import android.widget.ImageView; 8 | import android.widget.RatingBar; 9 | import android.widget.TextView; 10 | 11 | import androidx.appcompat.app.AppCompatActivity; 12 | 13 | import com.google.android.gms.ads.AdLoader; 14 | import com.google.android.gms.ads.AdRequest; 15 | import com.google.android.gms.ads.MobileAds; 16 | import com.google.android.gms.ads.formats.MediaView; 17 | import com.google.android.gms.ads.formats.UnifiedNativeAd; 18 | import com.google.android.gms.ads.formats.UnifiedNativeAdView; 19 | 20 | public class NativeActivity extends AppCompatActivity { 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_native); 26 | 27 | MobileAds.initialize(this); 28 | 29 | AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") 30 | .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() { 31 | @Override 32 | public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) { 33 | //the native ad will be available inside this method (unifiedNativeAd) 34 | 35 | UnifiedNativeAdView unifiedNativeAdView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.native_ad_layout, null); 36 | mapUnifiedNativeAdToLayout(unifiedNativeAd, unifiedNativeAdView); 37 | 38 | FrameLayout nativeAdLayout = findViewById(R.id.id_native_ad); 39 | nativeAdLayout.removeAllViews(); 40 | nativeAdLayout.addView(unifiedNativeAdView); 41 | } 42 | }) 43 | .build(); 44 | adLoader.loadAd(new AdRequest.Builder().build()); 45 | } 46 | 47 | public void mapUnifiedNativeAdToLayout(UnifiedNativeAd adFromGoogle, UnifiedNativeAdView myAdView) { 48 | MediaView mediaView = myAdView.findViewById(R.id.ad_media); 49 | myAdView.setMediaView(mediaView); 50 | 51 | myAdView.setHeadlineView(myAdView.findViewById(R.id.ad_headline)); 52 | myAdView.setBodyView(myAdView.findViewById(R.id.ad_body)); 53 | myAdView.setCallToActionView(myAdView.findViewById(R.id.ad_call_to_action)); 54 | myAdView.setIconView(myAdView.findViewById(R.id.ad_icon)); 55 | myAdView.setPriceView(myAdView.findViewById(R.id.ad_price)); 56 | myAdView.setStarRatingView(myAdView.findViewById(R.id.ad_rating)); 57 | myAdView.setStoreView(myAdView.findViewById(R.id.ad_store)); 58 | myAdView.setAdvertiserView(myAdView.findViewById(R.id.ad_advertiser)); 59 | 60 | ((TextView) myAdView.getHeadlineView()).setText(adFromGoogle.getHeadline()); 61 | 62 | if (adFromGoogle.getBody() == null) { 63 | myAdView.getBodyView().setVisibility(View.GONE); 64 | } else { 65 | ((TextView) myAdView.getBodyView()).setText(adFromGoogle.getBody()); 66 | } 67 | 68 | if (adFromGoogle.getCallToAction() == null) { 69 | myAdView.getCallToActionView().setVisibility(View.GONE); 70 | } else { 71 | ((Button) myAdView.getCallToActionView()).setText(adFromGoogle.getCallToAction()); 72 | } 73 | 74 | if (adFromGoogle.getIcon() == null) { 75 | myAdView.getIconView().setVisibility(View.GONE); 76 | } else { 77 | ((ImageView) myAdView.getIconView()).setImageDrawable(adFromGoogle.getIcon().getDrawable()); 78 | } 79 | 80 | if (adFromGoogle.getPrice() == null) { 81 | myAdView.getPriceView().setVisibility(View.GONE); 82 | } else { 83 | ((TextView) myAdView.getPriceView()).setText(adFromGoogle.getPrice()); 84 | } 85 | 86 | if (adFromGoogle.getStarRating() == null) { 87 | myAdView.getStarRatingView().setVisibility(View.GONE); 88 | } else { 89 | ((RatingBar) myAdView.getStarRatingView()).setRating(adFromGoogle.getStarRating().floatValue()); 90 | } 91 | 92 | if (adFromGoogle.getStore() == null) { 93 | myAdView.getStoreView().setVisibility(View.GONE); 94 | } else { 95 | ((TextView) myAdView.getStoreView()).setText(adFromGoogle.getStore()); 96 | } 97 | 98 | if (adFromGoogle.getAdvertiser() == null) { 99 | myAdView.getAdvertiserView().setVisibility(View.GONE); 100 | } else { 101 | ((TextView) myAdView.getAdvertiserView()).setText(adFromGoogle.getAdvertiser()); 102 | } 103 | 104 | myAdView.setNativeAd(adFromGoogle); 105 | } 106 | } 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /app/src/main/java/com/shaheershukur/admobtutorial/RewardedActivity.java: -------------------------------------------------------------------------------- 1 | package com.shaheershukur.admobtutorial; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | import android.widget.Button; 6 | import android.widget.TextView; 7 | 8 | import androidx.annotation.NonNull; 9 | import androidx.appcompat.app.AppCompatActivity; 10 | 11 | import com.google.android.gms.ads.AdRequest; 12 | import com.google.android.gms.ads.rewarded.RewardItem; 13 | import com.google.android.gms.ads.rewarded.RewardedAd; 14 | import com.google.android.gms.ads.rewarded.RewardedAdCallback; 15 | import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback; 16 | 17 | public class RewardedActivity extends AppCompatActivity { 18 | 19 | RewardedAd rewardedAd; 20 | Button videoButton; 21 | TextView adTextView; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_rewarded); 27 | 28 | videoButton = findViewById(R.id.ad_video_button); 29 | adTextView = findViewById(R.id.ad_text); 30 | 31 | rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917"); 32 | 33 | rewardedAd.loadAd(new AdRequest.Builder().build(), new RewardedAdLoadCallback() { 34 | @Override 35 | public void onRewardedAdLoaded() { 36 | super.onRewardedAdLoaded(); 37 | videoButton.setVisibility(View.VISIBLE); 38 | } 39 | }); 40 | 41 | videoButton.setOnClickListener(new View.OnClickListener() { 42 | @Override 43 | public void onClick(View view) { 44 | displayAd(); 45 | } 46 | }); 47 | } 48 | 49 | public void displayAd() { 50 | rewardedAd.show(this, new RewardedAdCallback() { 51 | @Override 52 | public void onUserEarnedReward(@NonNull RewardItem rewardItem) { 53 | videoButton.setVisibility(View.INVISIBLE); 54 | 55 | adTextView.setText("1 available"); 56 | } 57 | }); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/demo_screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/muhammed-shaheer-keerirakath/AdmobTutorialForAndroid/07a98fe2343e718d68feed28ca6bbb391ccff5f3/app/src/main/res/drawable/demo_screen.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_banner.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 21 | 22 | 23 | 28 | 29 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_interstitial.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 23 | 24 |