-
Notifications
You must be signed in to change notification settings - Fork 407
refactor(admob): add snippets from quickstart-android #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
03ad6d5
refactor(admob): add snippets from quickstart-android
thatfiredev 32d939d
refactor(admob): add gradle_play_config snippet
thatfiredev 61220e8
refactor(admob): enable multidex to fix build
thatfiredev 7659cb0
refactor(admob): remove snippets from Activity's onCreate
thatfiredev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason all of these snippets are in the
onCreate
function instead of in helper functions? Having most snippets live in their own function is generally easier to maintain, especially when they don't need to refer to each other's results/variables.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. And that actually leaves our onCreate snippet untouched.
I've updated the PR.