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
+23
-19
No files matched your search
@@ -21,7 +21,9 @@ import androidx.core.content.ContextCompat
|
|||||||
/* Ads */
|
/* Ads */
|
||||||
import com.google.android.gms.ads.AdRequest
|
import com.google.android.gms.ads.AdRequest
|
||||||
import com.google.android.gms.ads.AdSize
|
import com.google.android.gms.ads.AdSize
|
||||||
|
import com.google.android.gms.ads.AdView
|
||||||
import com.google.android.gms.ads.MobileAds
|
import com.google.android.gms.ads.MobileAds
|
||||||
|
import android.widget.FrameLayout
|
||||||
import com.google.android.gms.ads.FullScreenContentCallback
|
import com.google.android.gms.ads.FullScreenContentCallback
|
||||||
import com.google.android.gms.ads.rewarded.RewardedAd;
|
import com.google.android.gms.ads.rewarded.RewardedAd;
|
||||||
import com.google.android.ump.ConsentRequestParameters
|
import com.google.android.ump.ConsentRequestParameters
|
||||||
@@ -39,6 +41,9 @@ import com.jkanetwork.st.frasesdeanime.databinding.ActivityMainBinding
|
|||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
private var rewardedAd: RewardedAd? = null
|
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
|
private lateinit var binding: ActivityMainBinding
|
||||||
|
|
||||||
|
|
||||||
@@ -244,19 +249,22 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Banner adaptativo anclado abajo (sustituye al SMART_BANNER deprecated).
|
/** 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
|
* El AdView se crea por código con su adSize adaptativo: si viniera del XML sin
|
||||||
* XML, se comprueba aquí si ya llevan valor antes de asignar. */
|
* 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() {
|
private fun loadBannerAd() {
|
||||||
if (binding.adView.adUnitId == null) {
|
if (bannerAdView == null) {
|
||||||
binding.adView.adUnitId = getString(R.string.admob_bannerid)
|
bannerAdView = AdView(this).apply {
|
||||||
}
|
|
||||||
// AdSize trabaja en dp, no en píxeles físicos
|
// AdSize trabaja en dp, no en píxeles físicos
|
||||||
val adWidthDp = (resources.displayMetrics.widthPixels / resources.displayMetrics.density).toInt()
|
val adWidthDp = (resources.displayMetrics.widthPixels / resources.displayMetrics.density).toInt()
|
||||||
if (binding.adView.adSize == null) {
|
setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this@MainActivity, adWidthDp))
|
||||||
binding.adView.setAdSize(AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(
|
adUnitId = getString(R.string.admob_bannerid)
|
||||||
this, adWidthDp))
|
|
||||||
}
|
}
|
||||||
binding.adView.loadAd(AdRequest.Builder().build())
|
binding.adContainer.addView(
|
||||||
|
bannerAdView,
|
||||||
|
FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT))
|
||||||
|
}
|
||||||
|
bannerAdView?.loadAd(AdRequest.Builder().build())
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Carga el anuncio recompensado con el ID de UNIDAD (videoid), no el de app. */
|
/** Carga el anuncio recompensado con el ID de UNIDAD (videoid), no el de app. */
|
||||||
@@ -288,12 +296,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
changeData()
|
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,
|
* 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).
|
* y el diálogo del sistema tras 2 denegaciones deja de mostrarse: no insistir).
|
||||||
|
|||||||
@@ -156,12 +156,14 @@
|
|||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
|
<!-- Contenedor del banner: el AdView se crea por código (loadBannerAd) con tamaño
|
||||||
android:id="@+id/adView"
|
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_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentBottom="true"
|
android:layout_alignParentBottom="true"
|
||||||
android:layout_centerHorizontal="true"
|
android:layout_centerHorizontal="true" />
|
||||||
android:layout_marginBottom="0dp" />
|
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
Reference in new issue
Block a user