-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
531aae6
commit b634e62
Showing
8 changed files
with
324 additions
and
2 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
207 changes: 207 additions & 0 deletions
207
...pp/src/main/java/com/google/samples/quickstart/admobexample/kotlin/MainComposeActivity.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 |
---|---|---|
@@ -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()) | ||
} | ||
} | ||
) | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/ui/theme/Color.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 |
---|---|---|
@@ -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 |
11 changes: 11 additions & 0 deletions
11
admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/ui/theme/Shape.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 |
---|---|---|
@@ -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) | ||
) |
49 changes: 49 additions & 0 deletions
49
admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/ui/theme/Theme.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 |
---|---|---|
@@ -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 | ||
) | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
admob/app/src/main/java/com/google/samples/quickstart/admobexample/kotlin/ui/theme/Type.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 |
---|---|---|
@@ -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 | ||
) | ||
*/ | ||
) |
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