-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(admob): add snippets from quickstart-android (#272)
- Loading branch information
1 parent
f942733
commit 3cdf635
Showing
5 changed files
with
292 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
admob/app/src/main/java/com/google/firebase/example/admob/kotlin/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,125 @@ | ||
package com.google.firebase.example.admob.kotlin | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.Button | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.google.android.gms.ads.AdView | ||
import com.google.android.gms.ads.AdListener | ||
import com.google.android.gms.ads.AdRequest | ||
import com.google.android.gms.ads.InterstitialAd | ||
import com.google.android.gms.ads.LoadAdError | ||
import com.google.android.gms.ads.MobileAds | ||
import devrel.firebase.google.com.firebaseoptions.R | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
private lateinit var adView: AdView | ||
private lateinit var interstitialAd: InterstitialAd | ||
private lateinit var loadInterstitialButton: Button | ||
private val context = this | ||
|
||
// [START ads_on_create] | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
// ... | ||
MobileAds.initialize(this) | ||
} | ||
// [END ads_on_create] | ||
|
||
private fun loadAdBanner() { | ||
// [SNIPPET load_banner_ad] | ||
// Load an ad into the AdView. | ||
// [START load_banner_ad] | ||
|
||
// Initialize the Google Mobile Ads SDK | ||
MobileAds.initialize(context) | ||
|
||
val adRequest = AdRequest.Builder().build() | ||
|
||
adView.loadAd(adRequest) | ||
// [END load_banner_ad] | ||
} | ||
|
||
private fun initInterstitialAd() { | ||
// [START instantiate_interstitial_ad] | ||
// Create an InterstitialAd object. This same object can be re-used whenever you want to | ||
// show an interstitial. | ||
interstitialAd = InterstitialAd(context) | ||
interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id) | ||
// [END instantiate_interstitial_ad] | ||
} | ||
|
||
private fun createAdListener() { | ||
// [START create_interstitial_ad_listener] | ||
interstitialAd.adListener = object : AdListener() { | ||
override fun onAdClosed() { | ||
requestNewInterstitial() | ||
beginSecondActivity() | ||
} | ||
|
||
override fun onAdLoaded() { | ||
// Ad received, ready to display | ||
// ... | ||
} | ||
|
||
override fun onAdFailedToLoad(error: LoadAdError) { | ||
Log.w(TAG, "onAdFailedToLoad: ${error.message}") | ||
} | ||
} | ||
// [END create_interstitial_ad_listener] | ||
} | ||
|
||
private fun displayInterstitialAd() { | ||
// [START display_interstitial_ad] | ||
loadInterstitialButton.setOnClickListener { | ||
if (interstitialAd.isLoaded) { | ||
interstitialAd.show() | ||
} else { | ||
beginSecondActivity() | ||
} | ||
} | ||
// [END display_interstitial_ad] | ||
} | ||
|
||
/** | ||
* Load a new interstitial ad asynchronously. | ||
*/ | ||
// [START request_new_interstitial] | ||
private fun requestNewInterstitial() { | ||
val adRequest = AdRequest.Builder() | ||
.build() | ||
|
||
interstitialAd.loadAd(adRequest) | ||
} | ||
// [END request_new_interstitial] | ||
|
||
private fun beginSecondActivity() { } | ||
|
||
// [START add_lifecycle_methods] | ||
/** Called when leaving the activity */ | ||
public override fun onPause() { | ||
adView.pause() | ||
super.onPause() | ||
} | ||
|
||
/** Called when returning to the activity */ | ||
public override fun onResume() { | ||
super.onResume() | ||
adView.resume() | ||
if (!interstitialAd.isLoaded) { | ||
requestNewInterstitial() | ||
} | ||
} | ||
|
||
/** Called before the activity is destroyed */ | ||
public override fun onDestroy() { | ||
adView.destroy() | ||
super.onDestroy() | ||
} | ||
// [END add_lifecycle_methods] | ||
|
||
companion object { | ||
private const val TAG = "MainActivity" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,49 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/activity_main" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingLeft="@dimen/activity_horizontal_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
tools:context=".MainActivity"> | ||
<!-- [SNIPPET define_ad_layout] | ||
Define the namespace and AdView layout that will show the banner. | ||
[START define_ad_layout] --> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:ads="http://schemas.android.com/apk/res-auto" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<ImageView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="16dp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
|
||
<Button | ||
android:id="@+id/loadInterstitialButton" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="@string/interstitial_button_text" | ||
app:layout_constraintBottom_toTopOf="@+id/guideline" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="@+id/guideline" /> | ||
|
||
<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_ad_unit_id" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" /> | ||
|
||
<androidx.constraintlayout.widget.Guideline | ||
android:id="@+id/guideline" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:orientation="horizontal" | ||
app:layout_constraintGuide_percent="0.5" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout><!-- [END define_ad_layout] --> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Hello World!"/> | ||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
<resources> | ||
<string name="app_name">Firebase Options</string> | ||
<string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string> | ||
<string name="interstitial_button_text">Show Interstitial Ad</string> | ||
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string> | ||
</resources> |