Skip to content

Compose workspace #1452

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 13 commits into from
Feb 27, 2023
Merged
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
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
)
}

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

import androidx.compose.material.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp

// Set of Material typography styles to start with
val Typography = Typography(
body1 = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp
)
/* Other default text styles to override
titleLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 22.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp
),
labelSmall = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Medium,
fontSize = 11.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp
)
*/
)
3 changes: 3 additions & 0 deletions admob/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext {
compose_version = '1.3.0'
}
repositories {
mavenLocal()
google()
Expand Down