Admob Interstitial Ads
Here we are going to place Interstitial ads in our apps
Video Reference:
Example Code:
AndroidManifest.xml:
1234567891011121314151617181920212223242526
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.admobbannerad"> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.admobbannerad"> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
MainActivity.java:
1234567891011121314151617181920212223242526
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 package com.example.admobbannerad; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.google.android.gms.ads.AdListener; 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; public class MainActivity extends AppCompatActivity { private AdView mAdView; private InterstitialAd interstitialAd; private Button button; private Button button1; private InterstitialAd minterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest= new AdRequest.Builder().build(); mAdView.loadAd(adRequest); mAdView = (AdView) findViewById(R.id.adView1); AdRequest adRequest1= new AdRequest.Builder().build(); mAdView.loadAd(adRequest1); interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); interstitialAd.loadAd(new AdRequest.Builder().build()); minterstitialAd = new InterstitialAd(this); minterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); minterstitialAd.loadAd(new AdRequest.Builder().build()); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (interstitialAd.isLoaded()) { interstitialAd.show(); } else { Toast.makeText( MainActivity.this,"The interstitial wasn't loaded yet.",Toast.LENGTH_SHORT ).show(); } } }); button1 = (Button) findViewById(R.id.button2); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (minterstitialAd.isLoaded()) { minterstitialAd.show(); } else { Toast.makeText( MainActivity.this,"The interstitial wasn't loaded yet.",Toast.LENGTH_SHORT ).show(); } } }); interstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { // Load the next interstitial. interstitialAd.loadAd(new AdRequest.Builder().build()); } }); minterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { // Load the next interstitial. minterstitialAd.loadAd(new AdRequest.Builder().build()); } }); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | package com.example.admobbannerad; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.google.android.gms.ads.AdListener; 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; public class MainActivity extends AppCompatActivity { private AdView mAdView; private InterstitialAd interstitialAd; private Button button; private Button button1; private InterstitialAd minterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest= new AdRequest.Builder().build(); mAdView.loadAd(adRequest); mAdView = (AdView) findViewById(R.id.adView1); AdRequest adRequest1= new AdRequest.Builder().build(); mAdView.loadAd(adRequest1); interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); interstitialAd.loadAd(new AdRequest.Builder().build()); minterstitialAd = new InterstitialAd(this); minterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); minterstitialAd.loadAd(new AdRequest.Builder().build()); button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (interstitialAd.isLoaded()) { interstitialAd.show(); } else { Toast.makeText( MainActivity.this,"The interstitial wasn't loaded yet.",Toast.LENGTH_SHORT ).show(); } } }); button1 = (Button) findViewById(R.id.button2); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (minterstitialAd.isLoaded()) { minterstitialAd.show(); } else { Toast.makeText( MainActivity.this,"The interstitial wasn't loaded yet.",Toast.LENGTH_SHORT ).show(); } } }); interstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { // Load the next interstitial. interstitialAd.loadAd(new AdRequest.Builder().build()); } }); minterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { // Load the next interstitial. minterstitialAd.loadAd(new AdRequest.Builder().build()); } }); } } |
activity_main.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <?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" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_centerInParent="true" android:layout_marginEnd="6dp" android:layout_marginRight="6dp" android:text="Hello guys....Welcome to CYber World Technologies... Love you all" 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="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-3940256099942544/6300978111"> </com.google.android.gms.ads.AdView> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="127dp" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="119dp" android:text="Button" /> </RelativeLayout> |