Banner: AdView por codigo, no en XML sin adSize
El <AdView> del XML sin ads:adSize logueaba 'Required XML attribute adSize was missing' y fijaba SMART_BANNER; el setAdSize posterior no regeneraba el WebView (quedaba en altura 0). Ahora el XML lleva solo el contenedor y el AdView se crea con su adaptativo por codigo.
This commit is contained in:
1 parent
8e005b36db
commit
45acdb666f
2 files changed
+25
-21
No files matched your search
@@ -21,7 +21,9 @@ import androidx.core.content.ContextCompat
|
||||
/* Ads */
|
||||
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.MobileAds
|
||||
import android.widget.FrameLayout
|
||||
import com.google.android.gms.ads.FullScreenContentCallback
|
||||
import com.google.android.gms.ads.rewarded.RewardedAd;
|
||||
import com.google.android.ump.ConsentRequestParameters
|
||||
@@ -39,6 +41,9 @@ import com.jkanetwork.st.frasesdeanime.databinding.ActivityMainBinding
|
||||
class MainActivity : AppCompatActivity() {
|
||||
private var rewardedAd: RewardedAd? = null
|
||||
|
||||
/* Banner creado por código en loadBannerAd (el XML sólo lleva su contenedor) */
|
||||
private var bannerAdView: AdView? = null
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
|
||||
|
||||
@@ -244,19 +249,22 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
/** Banner adaptativo anclado abajo (sustituye al SMART_BANNER deprecated).
|
||||
* AdView exige que adUnitId y adSize se fijen UNA sola vez: como no van en el
|
||||
* XML, se comprueba aquí si ya llevan valor antes de asignar. */
|
||||
* El AdView se crea por código con su adSize adaptativo: si viniera del XML sin
|
||||
* ads:adSize, el SDK loguea "Required XML attribute adSize was missing" y
|
||||
* setAdSize posterior no regenera el WebView a la altura correcta. */
|
||||
private fun loadBannerAd() {
|
||||
if (binding.adView.adUnitId == null) {
|
||||
binding.adView.adUnitId = getString(R.string.admob_bannerid)
|
||||
if (bannerAdView == null) {
|
||||
bannerAdView = AdView(this).apply {
|
||||
// AdSize trabaja en dp, no en píxeles físicos
|
||||
val adWidthDp = (resources.displayMetrics.widthPixels / resources.displayMetrics.density).toInt()
|
||||
setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this@MainActivity, adWidthDp))
|
||||
adUnitId = getString(R.string.admob_bannerid)
|
||||
}
|
||||
binding.adContainer.addView(
|
||||
bannerAdView,
|
||||
FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT))
|
||||
}
|
||||
// AdSize trabaja en dp, no en píxeles físicos
|
||||
val adWidthDp = (resources.displayMetrics.widthPixels / resources.displayMetrics.density).toInt()
|
||||
if (binding.adView.adSize == null) {
|
||||
binding.adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
|
||||
this, adWidthDp))
|
||||
}
|
||||
binding.adView.loadAd(AdRequest.Builder().build())
|
||||
bannerAdView?.loadAd(AdRequest.Builder().build())
|
||||
}
|
||||
|
||||
/** Carga el anuncio recompensado con el ID de UNIDAD (videoid), no el de app. */
|
||||
@@ -288,12 +296,6 @@ class MainActivity : AppCompatActivity() {
|
||||
changeData()
|
||||
}
|
||||
|
||||
/** "Close" app on back button */
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun onBackPressed() {
|
||||
moveTaskToBack(true)
|
||||
}
|
||||
|
||||
/**
|
||||
* Pide POST_NOTIFICATIONS una sola vez (Android 13+ silencia las notificaciones sin él,
|
||||
* y el diálogo del sistema tras 2 denegaciones deja de mostrarse: no insistir).
|
||||
|
||||
@@ -156,12 +156,14 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/adView"
|
||||
<!-- Contenedor del banner: el AdView se crea por código (loadBannerAd) con tamaño
|
||||
adaptativo; si fuera un <AdView> del XML sin ads:adSize, el SDK loguea
|
||||
"Required XML attribute adSize was missing" y fija el suyo propio. -->
|
||||
<FrameLayout
|
||||
android:id="@+id/adContainer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_marginBottom="0dp" />
|
||||
android:layout_centerHorizontal="true" />
|
||||
|
||||
</RelativeLayout>
|
||||
Reference in new issue
Block a user