|
| 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 | +} |
0 commit comments