Skip to content

Commit b634e62

Browse files
create admob in compose (#1452)
1 parent 531aae6 commit b634e62

File tree

8 files changed

+324
-2
lines changed

8 files changed

+324
-2
lines changed

admob/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
<activity android:name=".java.MainActivity" />
3838
<activity android:name=".kotlin.MainActivity" />
39+
<activity android:name=".kotlin.MainComposeActivity" />
3940
<!-- [SNIPPET add_activity_config_changes]
4041
Include the AdActivity configChanges and theme.
4142
[START add_activity_config_changes] -->

admob/app/src/main/java/com/google/samples/quickstart/admobexample/EntryChoiceActivity.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ class EntryChoiceActivity : BaseEntryChoiceActivity() {
1212
Choice(
1313
"Java",
1414
"Run the Firebase Admob quickstart written in Java.",
15-
Intent(this, MainActivity::class.java)),
15+
Intent(this, com.google.samples.quickstart.admobexample.java.MainActivity::class.java)),
1616
Choice(
1717
"Kotlin",
1818
"Run the Firebase Admob quickstart written in Kotlin.",
19-
Intent(this, com.google.samples.quickstart.admobexample.kotlin.MainActivity::class.java))
19+
Intent(this, com.google.samples.quickstart.admobexample.kotlin.MainActivity::class.java)),
20+
Choice(
21+
"Compose",
22+
"Run the Firebase Admob quickstart written in Compose.",
23+
Intent(this, com.google.samples.quickstart.admobexample.kotlin.MainComposeActivity::class.java))
2024
)
2125
}
2226
}
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package com.google.samples.quickstart.admobexample.kotlin
2+
3+
import android.content.ContentValues.TAG
4+
import android.os.Bundle
5+
import android.util.Log
6+
import androidx.activity.ComponentActivity
7+
import androidx.activity.compose.setContent
8+
import androidx.compose.foundation.Image
9+
import androidx.compose.foundation.layout.Column
10+
import androidx.compose.foundation.layout.fillMaxSize
11+
import androidx.compose.foundation.layout.fillMaxHeight
12+
import androidx.compose.foundation.layout.fillMaxWidth
13+
import androidx.compose.foundation.layout.height
14+
import androidx.compose.foundation.layout.padding
15+
import androidx.compose.foundation.layout.Row
16+
import androidx.compose.foundation.layout.Spacer
17+
import androidx.compose.material.Surface
18+
import androidx.compose.material.TopAppBar
19+
import androidx.compose.material.MaterialTheme
20+
import androidx.compose.material.Scaffold
21+
import androidx.compose.material.Button
22+
import androidx.compose.material.ButtonDefaults
23+
import androidx.compose.material.Text
24+
import androidx.compose.runtime.Composable
25+
import androidx.compose.ui.Alignment
26+
import androidx.compose.ui.Modifier
27+
import androidx.compose.ui.graphics.Color
28+
import androidx.compose.ui.res.colorResource
29+
import androidx.compose.ui.res.painterResource
30+
import androidx.compose.ui.res.stringResource
31+
import androidx.compose.ui.text.style.TextAlign
32+
import androidx.compose.ui.unit.dp
33+
import androidx.compose.ui.unit.sp
34+
import androidx.compose.ui.viewinterop.AndroidView
35+
import com.google.android.gms.ads.AdError
36+
import com.google.android.gms.ads.AdRequest
37+
import com.google.android.gms.ads.AdSize
38+
import com.google.android.gms.ads.AdView
39+
import com.google.android.gms.ads.FullScreenContentCallback
40+
import com.google.android.gms.ads.LoadAdError
41+
import com.google.android.gms.ads.MobileAds
42+
import com.google.samples.quickstart.admobexample.kotlin.ui.theme.AdmobTheme
43+
import com.google.samples.quickstart.admobexample.R
44+
import com.google.android.gms.ads.interstitial.InterstitialAd
45+
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
46+
47+
class MainComposeActivity : ComponentActivity() {
48+
private var mInterstitialAd: InterstitialAd? = null
49+
private lateinit var adUnitId: String //="ca-app-pub-3940256099942544/1033173712" //could be set to another id
50+
private val buttonClickLambda = { displayNewInterstitial() }
51+
52+
override fun onCreate(savedInstanceState: Bundle?) {
53+
super.onCreate(savedInstanceState)
54+
adUnitId = getString(R.string.interstitial_ad_unit_id)
55+
56+
setContent {
57+
AdmobTheme {
58+
//A surface container using the 'background' color from the theme
59+
Surface(
60+
modifier = Modifier.fillMaxSize(),
61+
color = MaterialTheme.colors.background
62+
) {
63+
MainAppView( buttonClickEventAdLoader = buttonClickLambda ) // call Composable UI
64+
}
65+
}
66+
67+
initializeInterstitial()
68+
}
69+
}
70+
71+
private fun setInterstitialCallback() {
72+
73+
74+
mInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
75+
override fun onAdClicked() {
76+
// Called when a click is recorded for an ad.
77+
Log.d(TAG, "Ad was clicked.")
78+
}
79+
80+
override fun onAdDismissedFullScreenContent() {
81+
// Called when ad is dismissed.
82+
Log.d(TAG, "Ad dismissed fullscreen content.")
83+
mInterstitialAd = null
84+
initializeInterstitial() // get a new ad
85+
}
86+
87+
override fun onAdFailedToShowFullScreenContent(p0: AdError) {
88+
// Called when ad fails to show.
89+
Log.e(TAG, "Ad failed to show fullscreen content.")
90+
mInterstitialAd = null
91+
initializeInterstitial() // get a new ad
92+
}
93+
94+
override fun onAdImpression() {
95+
// Called when an impression is recorded for an ad.
96+
Log.d(TAG, "Ad recorded an impression.")
97+
}
98+
99+
override fun onAdShowedFullScreenContent() {
100+
// Called when ad is shown.
101+
Log.d(TAG, "Ad showed fullscreen content.")
102+
}
103+
}
104+
}
105+
106+
private fun initializeInterstitial(){
107+
MobileAds.initialize(this)
108+
val adRequest = AdRequest.Builder().build()
109+
110+
InterstitialAd.load(this, adUnitId, adRequest, object : InterstitialAdLoadCallback() {
111+
override fun onAdFailedToLoad(adError: LoadAdError) {
112+
Log.d(TAG, adError.toString())
113+
mInterstitialAd = null
114+
}
115+
116+
override fun onAdLoaded(interstitialAd: InterstitialAd) {
117+
Log.d(TAG, "Ad was loaded.")
118+
mInterstitialAd = interstitialAd
119+
}
120+
})
121+
}
122+
123+
fun displayNewInterstitial(){
124+
if (mInterstitialAd != null) { // ad is available
125+
setInterstitialCallback() // set the callback methods
126+
mInterstitialAd?.show(this)
127+
} else { // ad is not available
128+
Log.d("TAG", "The interstitial ad wasn't ready yet.")
129+
initializeInterstitial()
130+
}
131+
}
132+
133+
134+
}
135+
136+
137+
@Composable
138+
fun MainAppView(modifier: Modifier = Modifier, buttonClickEventAdLoader : () -> Unit = {}){
139+
Scaffold(
140+
topBar = { // top bar with app name
141+
TopAppBar(
142+
backgroundColor = colorResource(R.color.colorPrimary)
143+
) {
144+
androidx.compose.material.Text(
145+
text = stringResource(R.string.app_name),
146+
style = androidx.compose.material.MaterialTheme.typography.h6,
147+
textAlign = TextAlign.Center,
148+
modifier = Modifier.padding(8.dp),
149+
color = Color.White
150+
)
151+
}
152+
},
153+
content = {
154+
155+
Column(
156+
modifier = Modifier
157+
.padding(it)
158+
.fillMaxWidth()
159+
.fillMaxHeight(),
160+
161+
horizontalAlignment = Alignment.CenterHorizontally
162+
) {
163+
Spacer(modifier = Modifier.height(24.dp))
164+
165+
Image(painter = painterResource(R.drawable.firebase_lockup_400), contentDescription = "")
166+
Spacer(modifier = Modifier.height(160.dp))
167+
Button(
168+
colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(R.color.colorAccent)),
169+
onClick = { buttonClickEventAdLoader() } //lambda for onClick action
170+
) {
171+
Text(
172+
text = stringResource(R.string.interstitial_button_text),
173+
fontSize = 24.sp,
174+
color = Color.White
175+
)
176+
}
177+
178+
}
179+
},
180+
bottomBar = { // keeps the banner ad at the bottom!
181+
AdvertBanner()
182+
}
183+
)
184+
185+
}
186+
187+
188+
@Composable
189+
fun AdvertBanner(modifier: Modifier = Modifier) { // banner advert
190+
191+
Row(
192+
modifier = Modifier
193+
.fillMaxWidth()
194+
) {
195+
AndroidView(
196+
modifier = modifier.fillMaxWidth(),
197+
factory = { context ->
198+
AdView(context).apply {
199+
setAdSize(AdSize.BANNER)
200+
adUnitId = context.getString(R.string.banner_ad_unit_id)
201+
loadAd(AdRequest.Builder().build())
202+
}
203+
}
204+
)
205+
}
206+
207+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.google.samples.quickstart.admobexample.kotlin.ui.theme
2+
3+
import androidx.compose.ui.graphics.Color
4+
import com.google.samples.quickstart.admobexample.R
5+
6+
val Purple80 = Color(0xFFD0BCFF)
7+
val PurpleGrey80 = Color(0xFFCCC2DC)
8+
val Pink80 = Color(0xFFEFB8C8)
9+
10+
// self-defined light-mode colour scheme
11+
val FirebaseBlue = Color(0xFF0288D1) // copied from colors.xml
12+
val FirebaseBannerBlue = Color(0xFF039BE5) // copied from colors.xml
13+
val FirebaseOrange = Color(0xFFFFA000) // copied from colors.xml
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.google.samples.quickstart.admobexample.kotlin.ui.theme
2+
3+
import androidx.compose.foundation.shape.RoundedCornerShape
4+
import androidx.compose.material.Shapes
5+
import androidx.compose.ui.unit.dp
6+
7+
val Shapes = Shapes(
8+
small = RoundedCornerShape(16.dp),
9+
medium = RoundedCornerShape(4.dp),
10+
large = RoundedCornerShape(0.dp)
11+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.google.samples.quickstart.admobexample.kotlin.ui.theme
2+
3+
import androidx.compose.foundation.isSystemInDarkTheme
4+
import androidx.compose.material.MaterialTheme
5+
import androidx.compose.material.darkColors
6+
import androidx.compose.material.lightColors
7+
import androidx.compose.runtime.Composable
8+
9+
private val DarkColorPalette = darkColors(
10+
primary = Purple80,
11+
primaryVariant = PurpleGrey80,
12+
secondary = Pink80
13+
)
14+
15+
private val LightColorPalette = lightColors(
16+
primary = FirebaseBlue,
17+
primaryVariant = FirebaseBannerBlue,
18+
secondary = FirebaseOrange
19+
20+
/* Other default colors to override
21+
background = Color(0xFFFFFBFE),
22+
surface = Color(0xFFFFFBFE),
23+
onPrimary = Color.White,
24+
onSecondary = Color.White,
25+
onTertiary = Color.White,
26+
onBackground = Color(0xFF1C1B1F),
27+
onSurface = Color(0xFF1C1B1F),
28+
*/
29+
)
30+
31+
@Composable
32+
fun AdmobTheme(
33+
darkTheme: Boolean = isSystemInDarkTheme(),
34+
content: @Composable () -> Unit
35+
) {
36+
val colors = if (darkTheme) {
37+
DarkColorPalette
38+
} else {
39+
LightColorPalette
40+
}
41+
42+
MaterialTheme(
43+
colors = colors,
44+
typography = Typography,
45+
shapes = Shapes,
46+
content = content
47+
)
48+
}
49+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.google.samples.quickstart.admobexample.kotlin.ui.theme
2+
3+
import androidx.compose.material.Typography
4+
import androidx.compose.ui.text.TextStyle
5+
import androidx.compose.ui.text.font.FontFamily
6+
import androidx.compose.ui.text.font.FontWeight
7+
import androidx.compose.ui.unit.sp
8+
9+
// Set of Material typography styles to start with
10+
val Typography = Typography(
11+
body1 = TextStyle(
12+
fontFamily = FontFamily.Default,
13+
fontWeight = FontWeight.Normal,
14+
fontSize = 16.sp,
15+
lineHeight = 24.sp,
16+
letterSpacing = 0.5.sp
17+
)
18+
/* Other default text styles to override
19+
titleLarge = TextStyle(
20+
fontFamily = FontFamily.Default,
21+
fontWeight = FontWeight.Normal,
22+
fontSize = 22.sp,
23+
lineHeight = 28.sp,
24+
letterSpacing = 0.sp
25+
),
26+
labelSmall = TextStyle(
27+
fontFamily = FontFamily.Default,
28+
fontWeight = FontWeight.Medium,
29+
fontSize = 11.sp,
30+
lineHeight = 16.sp,
31+
letterSpacing = 0.5.sp
32+
)
33+
*/
34+
)

admob/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
ext {
5+
compose_version = '1.3.0'
6+
}
47
repositories {
58
mavenLocal()
69
google()

0 commit comments

Comments
 (0)