Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions admob/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ check.dependsOn 'assembleDebugAndroidTest'

android {
namespace 'com.google.samples.quickstart.admobexample'
compileSdkVersion 33
compileSdk 33

defaultConfig {
applicationId "com.google.samples.quickstart.admobexample"
minSdkVersion 19
targetSdkVersion 33
minSdk 21 // minSdk would be 19 without compose
targetSdk 33
versionCode 1
versionName "1.0"
multiDexEnabled true

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}

buildTypes {
Expand All @@ -36,6 +39,17 @@ android {

buildFeatures {
viewBinding = true
compose true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
composeOptions {
kotlinCompilerExtensionVersion '1.3.2'
}
}

Expand All @@ -47,19 +61,30 @@ dependencies {
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'

implementation 'com.google.android.gms:play-services-ads:21.3.0'

// Import the Firebase BoM (see: https://firebase.google.com/docs/android/learn-more#bom)
implementation platform('com.google.firebase:firebase-bom:31.1.0')
implementation platform('com.google.firebase:firebase-bom:31.0.2')

// For an optimal experience using AdMob, add the Firebase SDK
// for Google Analytics. This is recommended, but not required.
implementation 'com.google.firebase:firebase-analytics'

debugImplementation "androidx.fragment:fragment-testing:1.5.4"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test:runner:1.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
// Jetpack Compose
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.activity:activity-compose:1.5.1'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

debugImplementation "androidx.fragment:fragment-testing:1.5.2"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}
1 change: 1 addition & 0 deletions admob/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

<activity android:name=".java.MainActivity" />
<activity android:name=".kotlin.MainActivity" />
<activity android:name=".kotlin.MainComposeActivity" />
<!-- [SNIPPET add_activity_config_changes]
Include the AdActivity configChanges and theme.
[START add_activity_config_changes] -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ class EntryChoiceActivity : BaseEntryChoiceActivity() {
Choice(
"Java",
"Run the Firebase Admob quickstart written in Java.",
Intent(this, MainActivity::class.java)),
Intent(this, com.google.samples.quickstart.admobexample.java.MainActivity::class.java)),
Choice(
"Kotlin",
"Run the Firebase Admob quickstart written in Kotlin.",
Intent(this, com.google.samples.quickstart.admobexample.kotlin.MainActivity::class.java))
Intent(this, com.google.samples.quickstart.admobexample.kotlin.MainActivity::class.java)),
Choice(
"Compose",
"Run the Firebase Admob quickstart written in Compose.",
Intent(this, com.google.samples.quickstart.admobexample.kotlin.MainComposeActivity::class.java))
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package com.google.samples.quickstart.admobexample.kotlin

import android.content.ContentValues.TAG
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.material.Surface
import androidx.compose.material.TopAppBar
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.MobileAds
import com.google.samples.quickstart.admobexample.kotlin.ui.theme.AdmobTheme
import com.google.samples.quickstart.admobexample.R
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback

class MainComposeActivity : ComponentActivity() {
private var mInterstitialAd: InterstitialAd? = null
private lateinit var adUnitId: String //="ca-app-pub-3940256099942544/1033173712" //could be set to another id
private val buttonClickLambda = { displayNewInterstitial() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
adUnitId = getString(R.string.interstitial_ad_unit_id)

setContent {
AdmobTheme {
//A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
MainAppView( buttonClickEventAdLoader = buttonClickLambda ) // call Composable UI
}
}

initializeInterstitial()
}
}

private fun setInterstitialCallback() {


mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}

override fun onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
Log.d(TAG, "Ad dismissed fullscreen content.")
mInterstitialAd = null
initializeInterstitial() // get a new ad
}

override fun onAdFailedToShowFullScreenContent(p0: AdError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
mInterstitialAd = null
initializeInterstitial() // get a new ad
}

override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}

override fun onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
}
}

private fun initializeInterstitial(){
MobileAds.initialize(this)
val adRequest = AdRequest.Builder().build()

InterstitialAd.load(this, adUnitId, adRequest, object : InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError.toString())
mInterstitialAd = null
}

override fun onAdLoaded(interstitialAd: InterstitialAd) {
Log.d(TAG, "Ad was loaded.")
mInterstitialAd = interstitialAd
}
})
}

fun displayNewInterstitial(){
if (mInterstitialAd != null) { // ad is available
setInterstitialCallback() // set the callback methods
mInterstitialAd?.show(this)
} else { // ad is not available
Log.d("TAG", "The interstitial ad wasn't ready yet.")
initializeInterstitial()
}
}


}


@Composable
fun MainAppView(modifier: Modifier = Modifier, buttonClickEventAdLoader : () -> Unit = {}){
Scaffold(
topBar = { // top bar with app name
TopAppBar(
backgroundColor = colorResource(R.color.colorPrimary)
) {
androidx.compose.material.Text(
text = stringResource(R.string.app_name),
style = androidx.compose.material.MaterialTheme.typography.h6,
textAlign = TextAlign.Center,
modifier = Modifier.padding(8.dp),
color = Color.White
)
}
},
content = {

Column(
modifier = Modifier
.padding(it)
.fillMaxWidth()
.fillMaxHeight(),

horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.height(24.dp))

Image(painter = painterResource(R.drawable.firebase_lockup_400), contentDescription = "")
Spacer(modifier = Modifier.height(160.dp))
Button(
colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.colorAccent)),
onClick = { buttonClickEventAdLoader() } //lambda for onClick action
) {
Text(
text = stringResource(R.string.interstitial_button_text),
fontSize = 24.sp,
color = Color.White
)
}

}
},
bottomBar = { // keeps the banner ad at the bottom!
AdvertBanner()
}
)

}


@Composable
fun AdvertBanner(modifier: Modifier = Modifier) { // banner advert

Row(
modifier = Modifier
.fillMaxWidth()
) {
AndroidView(
modifier = modifier.fillMaxWidth(),
factory = { context ->
AdView(context).apply {
setAdSize(AdSize.BANNER)
adUnitId = context.getString(R.string.banner_ad_unit_id)
loadAd(AdRequest.Builder().build())
}
}
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.samples.quickstart.admobexample.kotlin.ui.theme

import androidx.compose.ui.graphics.Color
import com.google.samples.quickstart.admobexample.R

val Purple80 = Color(0xFFD0BCFF)
val PurpleGrey80 = Color(0xFFCCC2DC)
val Pink80 = Color(0xFFEFB8C8)

// self-defined light-mode colour scheme
val FirebaseBlue = Color(0xFF0288D1) // copied from colors.xml
val FirebaseBannerBlue = Color(0xFF039BE5) // copied from colors.xml
val FirebaseOrange = Color(0xFFFFA000) // copied from colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.google.samples.quickstart.admobexample.kotlin.ui.theme

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Shapes
import androidx.compose.ui.unit.dp

val Shapes = Shapes(
small = RoundedCornerShape(16.dp),
medium = RoundedCornerShape(4.dp),
large = RoundedCornerShape(0.dp)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.google.samples.quickstart.admobexample.kotlin.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable

private val DarkColorPalette = darkColors(
primary = Purple80,
primaryVariant = PurpleGrey80,
secondary = Pink80
)

private val LightColorPalette = lightColors(
primary = FirebaseBlue,
primaryVariant = FirebaseBannerBlue,
secondary = FirebaseOrange

/* Other default colors to override
background = Color(0xFFFFFBFE),
surface = Color(0xFFFFFBFE),
onPrimary = Color.White,
onSecondary = Color.White,
onTertiary = Color.White,
onBackground = Color(0xFF1C1B1F),
onSurface = Color(0xFF1C1B1F),
*/
)

@Composable
fun AdmobTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}

MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes,
content = content
)
}

Loading