Skip to content

Commit 3cdf635

Browse files
authored
refactor(admob): add snippets from quickstart-android (firebase#272)
1 parent f942733 commit 3cdf635

File tree

5 files changed

+292
-16
lines changed

5 files changed

+292
-16
lines changed

admob/app/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ android {
1212
versionCode 1
1313
versionName "1.0"
1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15+
multiDexEnabled true
1516
}
1617
buildTypes {
1718
release {
@@ -26,6 +27,12 @@ dependencies {
2627
implementation 'androidx.browser:browser:1.0.0'
2728
implementation 'androidx.appcompat:appcompat:1.2.0'
2829
implementation "com.google.firebase:firebase-ads:19.6.0"
30+
implementation "androidx.constraintlayout:constraintlayout:2.0.4"
31+
implementation "androidx.multidex:multidex:2.0.1"
32+
33+
// [START gradle_play_config]
34+
implementation 'com.google.android.gms:play-services-ads:19.6.0'
35+
// [END gradle_play_config]
2936

3037
// For an optimal experience using AdMob, add the Firebase SDK
3138
// for Google Analytics. This is recommended, but not required.

admob/app/src/main/java/com/google/firebase/example/admob/MainActivity.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
package com.google.firebase.example.admob;
22

3+
import android.content.Context;
34
import android.os.Bundle;
5+
import android.util.Log;
6+
import android.view.View;
7+
import android.widget.Button;
8+
49
import androidx.appcompat.app.AppCompatActivity;
510

11+
import com.google.android.gms.ads.AdListener;
12+
import com.google.android.gms.ads.AdRequest;
13+
import com.google.android.gms.ads.AdView;
14+
import com.google.android.gms.ads.InterstitialAd;
15+
import com.google.android.gms.ads.LoadAdError;
616
import com.google.android.gms.ads.MobileAds;
717

18+
import devrel.firebase.google.com.firebaseoptions.R;
19+
820
public class MainActivity extends AppCompatActivity {
921

22+
private static final String TAG = "MainActivity";
23+
24+
private AdView mAdView;
25+
private InterstitialAd mInterstitialAd;
26+
private Button mLoadInterstitialButton;
27+
private Context context = this;
28+
1029
// [START ads_on_create]
1130
@Override
1231
protected void onCreate(Bundle savedInstanceState) {
@@ -16,4 +35,111 @@ protected void onCreate(Bundle savedInstanceState) {
1635
}
1736
// [END ads_on_create]
1837

38+
private void loadBannerAd() {
39+
// [SNIPPET load_banner_ad]
40+
// Load an ad into the AdView.
41+
// [START load_banner_ad]
42+
43+
// Initialize the Google Mobile Ads SDK
44+
MobileAds.initialize(context);
45+
46+
AdRequest adRequest = new AdRequest.Builder().build();
47+
mAdView.loadAd(adRequest);
48+
// [END load_banner_ad]
49+
}
50+
51+
private void initInterstitialAd() {
52+
// [START instantiate_interstitial_ad]
53+
// Create an InterstitialAd object. This same object can be re-used whenever you want to
54+
// show an interstitial.
55+
mInterstitialAd = new InterstitialAd(context);
56+
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
57+
// [END instantiate_interstitial_ad]
58+
}
59+
60+
private void createInterstitialAdListener() {
61+
// [START create_interstitial_ad_listener]
62+
mInterstitialAd.setAdListener(new AdListener() {
63+
@Override
64+
public void onAdClosed() {
65+
requestNewInterstitial();
66+
beginSecondActivity();
67+
}
68+
69+
@Override
70+
public void onAdLoaded() {
71+
// Ad received, ready to display
72+
// ...
73+
}
74+
75+
@Override
76+
public void onAdFailedToLoad(LoadAdError loadAdError) {
77+
Log.w(TAG, "onAdFailedToLoad:" + loadAdError.getMessage());
78+
}
79+
});
80+
// [END create_interstitial_ad_listener]
81+
}
82+
83+
private void displayInterstitialAd() {
84+
// [START display_interstitial_ad]
85+
mLoadInterstitialButton = findViewById(R.id.loadInterstitialButton);
86+
mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() {
87+
@Override
88+
public void onClick(View v) {
89+
if (mInterstitialAd.isLoaded()) {
90+
mInterstitialAd.show();
91+
} else {
92+
beginSecondActivity();
93+
}
94+
}
95+
});
96+
// [END display_interstitial_ad]
97+
}
98+
99+
/**
100+
* Load a new interstitial ad asynchronously.
101+
*/
102+
// [START request_new_interstitial]
103+
private void requestNewInterstitial() {
104+
AdRequest adRequest = new AdRequest.Builder()
105+
.build();
106+
107+
mInterstitialAd.loadAd(adRequest);
108+
}
109+
// [END request_new_interstitial]
110+
111+
private void beginSecondActivity() { }
112+
113+
// [START add_lifecycle_methods]
114+
/** Called when leaving the activity */
115+
@Override
116+
public void onPause() {
117+
if (mAdView != null) {
118+
mAdView.pause();
119+
}
120+
super.onPause();
121+
}
122+
123+
/** Called when returning to the activity */
124+
@Override
125+
public void onResume() {
126+
super.onResume();
127+
if (mAdView != null) {
128+
mAdView.resume();
129+
}
130+
if (!mInterstitialAd.isLoaded()) {
131+
requestNewInterstitial();
132+
}
133+
}
134+
135+
/** Called before the activity is destroyed */
136+
@Override
137+
public void onDestroy() {
138+
if (mAdView != null) {
139+
mAdView.destroy();
140+
}
141+
super.onDestroy();
142+
}
143+
// [END add_lifecycle_methods]
144+
19145
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,125 @@
11
package com.google.firebase.example.admob.kotlin
22

33
import android.os.Bundle
4+
import android.util.Log
5+
import android.widget.Button
46
import androidx.appcompat.app.AppCompatActivity
7+
import com.google.android.gms.ads.AdView
8+
import com.google.android.gms.ads.AdListener
9+
import com.google.android.gms.ads.AdRequest
10+
import com.google.android.gms.ads.InterstitialAd
11+
import com.google.android.gms.ads.LoadAdError
512
import com.google.android.gms.ads.MobileAds
13+
import devrel.firebase.google.com.firebaseoptions.R
614

715
class MainActivity : AppCompatActivity() {
816

17+
private lateinit var adView: AdView
18+
private lateinit var interstitialAd: InterstitialAd
19+
private lateinit var loadInterstitialButton: Button
20+
private val context = this
21+
922
// [START ads_on_create]
1023
override fun onCreate(savedInstanceState: Bundle?) {
1124
super.onCreate(savedInstanceState)
1225
// ...
1326
MobileAds.initialize(this)
1427
}
1528
// [END ads_on_create]
29+
30+
private fun loadAdBanner() {
31+
// [SNIPPET load_banner_ad]
32+
// Load an ad into the AdView.
33+
// [START load_banner_ad]
34+
35+
// Initialize the Google Mobile Ads SDK
36+
MobileAds.initialize(context)
37+
38+
val adRequest = AdRequest.Builder().build()
39+
40+
adView.loadAd(adRequest)
41+
// [END load_banner_ad]
42+
}
43+
44+
private fun initInterstitialAd() {
45+
// [START instantiate_interstitial_ad]
46+
// Create an InterstitialAd object. This same object can be re-used whenever you want to
47+
// show an interstitial.
48+
interstitialAd = InterstitialAd(context)
49+
interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id)
50+
// [END instantiate_interstitial_ad]
51+
}
52+
53+
private fun createAdListener() {
54+
// [START create_interstitial_ad_listener]
55+
interstitialAd.adListener = object : AdListener() {
56+
override fun onAdClosed() {
57+
requestNewInterstitial()
58+
beginSecondActivity()
59+
}
60+
61+
override fun onAdLoaded() {
62+
// Ad received, ready to display
63+
// ...
64+
}
65+
66+
override fun onAdFailedToLoad(error: LoadAdError) {
67+
Log.w(TAG, "onAdFailedToLoad: ${error.message}")
68+
}
69+
}
70+
// [END create_interstitial_ad_listener]
71+
}
72+
73+
private fun displayInterstitialAd() {
74+
// [START display_interstitial_ad]
75+
loadInterstitialButton.setOnClickListener {
76+
if (interstitialAd.isLoaded) {
77+
interstitialAd.show()
78+
} else {
79+
beginSecondActivity()
80+
}
81+
}
82+
// [END display_interstitial_ad]
83+
}
84+
85+
/**
86+
* Load a new interstitial ad asynchronously.
87+
*/
88+
// [START request_new_interstitial]
89+
private fun requestNewInterstitial() {
90+
val adRequest = AdRequest.Builder()
91+
.build()
92+
93+
interstitialAd.loadAd(adRequest)
94+
}
95+
// [END request_new_interstitial]
96+
97+
private fun beginSecondActivity() { }
98+
99+
// [START add_lifecycle_methods]
100+
/** Called when leaving the activity */
101+
public override fun onPause() {
102+
adView.pause()
103+
super.onPause()
104+
}
105+
106+
/** Called when returning to the activity */
107+
public override fun onResume() {
108+
super.onResume()
109+
adView.resume()
110+
if (!interstitialAd.isLoaded) {
111+
requestNewInterstitial()
112+
}
113+
}
114+
115+
/** Called before the activity is destroyed */
116+
public override fun onDestroy() {
117+
adView.destroy()
118+
super.onDestroy()
119+
}
120+
// [END add_lifecycle_methods]
121+
122+
companion object {
123+
private const val TAG = "MainActivity"
124+
}
16125
}

admob/app/src/main/res/layout/activity_main.xml

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout
3-
xmlns:android="http://schemas.android.com/apk/res/android"
4-
xmlns:tools="http://schemas.android.com/tools"
5-
android:id="@+id/activity_main"
6-
android:layout_width="match_parent"
7-
android:layout_height="match_parent"
8-
android:paddingLeft="@dimen/activity_horizontal_margin"
9-
android:paddingRight="@dimen/activity_horizontal_margin"
10-
android:paddingTop="@dimen/activity_vertical_margin"
11-
android:paddingBottom="@dimen/activity_vertical_margin"
12-
tools:context=".MainActivity">
2+
<!-- [SNIPPET define_ad_layout]
3+
Define the namespace and AdView layout that will show the banner.
4+
[START define_ad_layout] -->
5+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
6+
xmlns:ads="http://schemas.android.com/apk/res-auto"
7+
xmlns:app="http://schemas.android.com/apk/res-auto"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent">
10+
11+
<ImageView
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_marginTop="16dp"
15+
app:layout_constraintEnd_toEndOf="parent"
16+
app:layout_constraintStart_toStartOf="parent"
17+
app:layout_constraintTop_toTopOf="parent" />
18+
19+
<Button
20+
android:id="@+id/loadInterstitialButton"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:text="@string/interstitial_button_text"
24+
app:layout_constraintBottom_toTopOf="@+id/guideline"
25+
app:layout_constraintEnd_toEndOf="parent"
26+
app:layout_constraintStart_toStartOf="parent"
27+
app:layout_constraintTop_toTopOf="@+id/guideline" />
28+
29+
<com.google.android.gms.ads.AdView
30+
android:id="@+id/adView"
31+
android:layout_width="wrap_content"
32+
android:layout_height="wrap_content"
33+
android:layout_alignParentBottom="true"
34+
android:layout_centerHorizontal="true"
35+
ads:adSize="BANNER"
36+
ads:adUnitId="@string/banner_ad_unit_id"
37+
app:layout_constraintBottom_toBottomOf="parent"
38+
app:layout_constraintEnd_toEndOf="parent"
39+
app:layout_constraintStart_toStartOf="parent" />
40+
41+
<androidx.constraintlayout.widget.Guideline
42+
android:id="@+id/guideline"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:orientation="horizontal"
46+
app:layout_constraintGuide_percent="0.5" />
47+
48+
</androidx.constraintlayout.widget.ConstraintLayout><!-- [END define_ad_layout] -->
1349

14-
<TextView
15-
android:layout_width="wrap_content"
16-
android:layout_height="wrap_content"
17-
android:text="Hello World!"/>
18-
</RelativeLayout>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<resources>
22
<string name="app_name">Firebase Options</string>
3+
<string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
4+
<string name="interstitial_button_text">Show Interstitial Ad</string>
5+
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
36
</resources>

0 commit comments

Comments
 (0)