Skip to content

Commit

Permalink
Layout params fix (#232)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* wip
  • Loading branch information
github-lucas-bon authored Sep 13, 2024
1 parent d3bea21 commit f9922f4
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ open class GenericRecyclerViewAdapter(private val mTitle: String) : RecyclerView
0 -> RecyclerItemType.TYPE_SCROLL_DOWN.value
1 -> RecyclerItemType.TYPE_ARTICLE_TITLE.value
2 -> RecyclerItemType.TYPE_ARTICLE_REAL_LINES.value
6 -> RecyclerItemType.TYPE_TEADS.value
4 -> RecyclerItemType.TYPE_TEADS.value
else -> RecyclerItemType.TYPE_ARTICLE_FAKE_LINES.value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import tv.teads.sdk.TeadsMediationSettings
import tv.teads.teadssdkdemo.R
import tv.teads.teadssdkdemo.data.CreativeSize
import tv.teads.teadssdkdemo.databinding.FragmentInreadScrollviewBinding
import tv.teads.teadssdkdemo.format.inread.extensions.resizeAdContainer
import tv.teads.teadssdkdemo.format.mediation.identifier.PrebidIdentifier
import tv.teads.teadssdkdemo.utils.BaseFragment

Expand Down Expand Up @@ -69,12 +70,8 @@ class PluginRendererScrollViewFragment : BaseFragment() {
bannerView?.setPluginEventListener(object : TeadsPBMEventListener{
override fun onAdRatioUpdate(adRatio: AdRatio) {
Log.d("TeadsPBMEventListener", "onAdRatioUpdate")

bannerView?.let {
val adViewParams = binding.adSlotView.layoutParams
adViewParams.height = adRatio.calculateHeight(it.measuredWidth)
binding.adSlotView.layoutParams = adViewParams
}
// Resize
binding.adSlotContainer.resizeAdContainer(adRatio)
}

override fun onAdCollapsedFromFullscreen() {
Expand Down Expand Up @@ -114,7 +111,7 @@ class PluginRendererScrollViewFragment : BaseFragment() {
})

// 8. Add the ad view to its container
bannerView?.let { binding.adSlotView.addView(it) }
bannerView?.let { binding.adSlotContainer.addView(it) }

// 9. Load the ad
bannerView?.loadAd()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import tv.teads.sdk.VideoPlaybackListener
import tv.teads.sdk.renderer.InReadAdView
import tv.teads.teadssdkdemo.R
import tv.teads.teadssdkdemo.databinding.FragmentInreadScrollviewBinding
import tv.teads.teadssdkdemo.format.inread.extensions.resizeAdContainer
import tv.teads.teadssdkdemo.utils.BaseFragment

/**
Expand Down Expand Up @@ -62,24 +63,21 @@ class StandaloneIntegrationScrollViewFragment : BaseFragment() {
requestSettings,
object : InReadAdViewListener {
override fun onAdReceived(ad: InReadAdView, adRatio: AdRatio) {
val layoutParams = ad.layoutParams
binding.adSlotView.addView(ad)
layoutParams.height = adRatio.calculateHeight(binding.adSlotView.measuredWidth)
binding.adSlotView.layoutParams = layoutParams

// Clean and init inReadAdView
inReadAdView?.clean()
inReadAdView = ad
// Add ad to the container and resize
binding.adSlotContainer.addView(ad)
binding.adSlotContainer.resizeAdContainer(adRatio)
}

override fun adOpportunityTrackerView(trackerView: AdOpportunityTrackerView) {
binding.adSlotView.addView(trackerView)
binding.adSlotContainer.addView(trackerView)
}

override fun onAdRatioUpdate(adRatio: AdRatio) {
inReadAdView?.let { inReadAdView ->
val layoutParams = inReadAdView.layoutParams
layoutParams.height = adRatio.calculateHeight(binding.adSlotView.measuredWidth)
binding.adSlotView.layoutParams = layoutParams
}
// Resize
binding.adSlotContainer.resizeAdContainer(adRatio)
}

override fun onAdClicked() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import androidx.recyclerview.widget.RecyclerView
import tv.teads.teadssdkdemo.databinding.FragmentRecyclerviewBinding
import tv.teads.teadssdkdemo.format.inread.adapter.SimpleRecyclerViewAdapter
import tv.teads.teadssdkdemo.utils.BaseFragment

/**
*
* inRead format within a RecyclerView
*
*/
class InReadGridRecyclerViewFragment : BaseFragment() {
private lateinit var binding: FragmentRecyclerviewBinding
private lateinit var adapter: SimpleRecyclerViewAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import tv.teads.sdk.VideoPlaybackListener
import tv.teads.sdk.renderer.InReadAdView
import tv.teads.teadssdkdemo.R
import tv.teads.teadssdkdemo.databinding.FragmentInreadScrollviewBinding
import tv.teads.teadssdkdemo.format.inread.extensions.resizeAdContainer
import tv.teads.teadssdkdemo.utils.BaseFragment

/**
Expand All @@ -41,61 +42,59 @@ class InReadScrollViewFragment : BaseFragment() {

// 1. Setup the settings
val placementSettings = AdPlacementSettings.Builder()
.enableDebug()
.build()
.enableDebug()
.build()

// 2. Create the InReadAdPlacement
adPlacement = TeadsSDK.createInReadPlacement(requireActivity(), pid, placementSettings)

// 3. Request the ad and listen its events
val requestSettings = AdRequestSettings.Builder()
.pageSlotUrl("http://teads.com")
.build()
.pageSlotUrl("http://teads.com")
.build()
adPlacement.requestAd(requestSettings,
object : InReadAdViewListener {
override fun onAdReceived(ad: InReadAdView, adRatio: AdRatio) {
val layoutParams = ad.layoutParams
binding.adSlotView.addView(ad)
layoutParams.height = adRatio.calculateHeight(binding.adSlotView.measuredWidth)
binding.adSlotView.layoutParams = layoutParams

inReadAdView = ad
}

override fun adOpportunityTrackerView(trackerView: AdOpportunityTrackerView) {
binding.adSlotView.addView(trackerView)
}

override fun onAdRatioUpdate(adRatio: AdRatio) {
inReadAdView?.let { inReadAdView ->
val layoutParams = inReadAdView.layoutParams
layoutParams.height = adRatio.calculateHeight(binding.adSlotView.measuredWidth)
binding.adSlotView.layoutParams = layoutParams
}
}

override fun onAdClicked() {}
override fun onAdClosed() {}
override fun onAdError(code: Int, description: String) {}
override fun onAdImpression() {}
override fun onAdExpandedToFullscreen() {}
override fun onAdCollapsedFromFullscreen() {}
override fun onFailToReceiveAd(failReason: String) {}
},
object : VideoPlaybackListener {
override fun onVideoComplete() {
Log.d("PlaybackEvent", "complete")
}

override fun onVideoPause() {
Log.d("PlaybackEvent", "pause")
}

override fun onVideoPlay() {
Log.d("PlaybackEvent", "play")
}
object : InReadAdViewListener {
override fun onAdReceived(ad: InReadAdView, adRatio: AdRatio) {
// Clean and init inReadAdView
inReadAdView?.clean()
inReadAdView = ad
// Add ad to the container and resize
binding.adSlotContainer.addView(ad)
binding.adSlotContainer.resizeAdContainer(adRatio)
}

override fun adOpportunityTrackerView(trackerView: AdOpportunityTrackerView) {
// Resize ad container
binding.adSlotContainer.addView(trackerView)
}

override fun onAdRatioUpdate(adRatio: AdRatio) {
// Resize the ad container
binding.adSlotContainer.resizeAdContainer(adRatio)
}

override fun onAdClicked() {}
override fun onAdClosed() {}
override fun onAdError(code: Int, description: String) {}
override fun onAdImpression() {}
override fun onAdExpandedToFullscreen() {}
override fun onAdCollapsedFromFullscreen() {}
override fun onFailToReceiveAd(failReason: String) {}
},
object : VideoPlaybackListener {
override fun onVideoComplete() {
Log.d("PlaybackEvent", "complete")
}

override fun onVideoPause() {
Log.d("PlaybackEvent", "pause")
}

override fun onVideoPlay() {
Log.d("PlaybackEvent", "play")
}

}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tv.teads.sdk.*
import tv.teads.sdk.renderer.InReadAdView
import tv.teads.teadssdkdemo.component.GenericRecyclerViewAdapter
import tv.teads.teadssdkdemo.data.RecyclerItemType
import tv.teads.teadssdkdemo.format.inread.extensions.resizeAdContainer

/**
* Simple RecyclerView adapter
Expand Down Expand Up @@ -41,30 +42,24 @@ class SimpleRecyclerViewAdapter(private val context: Context?, pid: Int, title:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
RecyclerItemType.TYPE_TEADS.value -> {
val adViewContainer = holder.itemView as FrameLayout
var inReadAdView: InReadAdView? = null
val adSlotContainer = holder.itemView as FrameLayout
adSlotContainer.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)

// 3. Request the ad and register to the listener in it
adPlacement.requestAd(requestSettings, object : InReadAdViewListener {
override fun adOpportunityTrackerView(trackerView: AdOpportunityTrackerView) {
adViewContainer.addView(trackerView)
adSlotContainer.addView(trackerView)
}

override fun onAdReceived(ad: InReadAdView, adRatio: AdRatio) {
val layoutParams = ad.layoutParams
adViewContainer.addView(ad, 0)
layoutParams.height = adRatio.calculateHeight(adViewContainer.measuredWidth)
adViewContainer.layoutParams = layoutParams

inReadAdView = ad
// Add ad to the container and resize
adSlotContainer.resizeAdContainer(adRatio)
adSlotContainer.addView(ad, 0)
}

override fun onAdRatioUpdate(adRatio: AdRatio) {
inReadAdView?.let { inReadAdView ->
val layoutParams = inReadAdView.layoutParams
layoutParams.height = adRatio.calculateHeight(adViewContainer.measuredWidth)
adViewContainer.layoutParams = layoutParams
}
// Resize
adSlotContainer.resizeAdContainer(adRatio)
}

override fun onAdClicked() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tv.teads.teadssdkdemo.format.inread.extensions

import android.view.View
import tv.teads.sdk.AdRatio

/**
* Resize the ad container to the correct height
* @param adRatio the ratio of the ad
*
* adRatio.calculateHeight(adSlotContainer.measuredWidth) calculates the height of the ad container
* measuredWidth parameter must to be from the ad container/view group encapsulating the ad view
*/
fun View.resizeAdContainer(adRatio: AdRatio) {
val adSlotContainer = this
val adSlotContainerParams = adSlotContainer.layoutParams
adSlotContainerParams.height = adRatio.calculateHeight(adSlotContainer.measuredWidth)
adSlotContainer.layoutParams = adSlotContainerParams
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ import tv.teads.sdk.utils.userConsent.TCFVersion
import tv.teads.teadssdkdemo.component.GenericRecyclerViewAdapter
import tv.teads.teadssdkdemo.data.RecyclerItemType
import tv.teads.teadssdkdemo.data.SessionDataSource
import tv.teads.teadssdkdemo.format.inread.extensions.resizeAdContainer

/**
* Simple RecyclerView adapter
*/
class AdMobRecyclerViewAdapter(admobBannerId: String, context: Context?, title: String)
class AdMobRecyclerViewAdapter(admobBannerId: String, context: Context, title: String)
: GenericRecyclerViewAdapter(title) {

private val adView: AdView = AdView(context!!)
private val adView: AdView = AdView(context)
private val mListener: TeadsAdapterListener

init {
// 1. Initialize AdMob & Teads Helper
MobileAds.initialize(context!!)
MobileAds.initialize(context)
TeadsHelper.initialize()

// 2. Setup the AdMob view
Expand Down Expand Up @@ -67,12 +68,7 @@ class AdMobRecyclerViewAdapter(admobBannerId: String, context: Context?, title:
adView.viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
adView.viewTreeObserver.removeOnGlobalLayoutListener(this)
val params: ViewGroup.LayoutParams = adView.layoutParams

// Here the width is MATCH_PARENT
params.height = adRatio.calculateHeight(adView.measuredWidth)

adView.layoutParams = params
adView.resizeAdContainer(adRatio)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import tv.teads.sdk.utils.userConsent.TCFVersion
import tv.teads.teadssdkdemo.component.GenericRecyclerViewAdapter
import tv.teads.teadssdkdemo.data.RecyclerItemType
import tv.teads.teadssdkdemo.data.SessionDataSource
import tv.teads.teadssdkdemo.format.inread.extensions.resizeAdContainer

/**
* Simple RecyclerView adapter
*/
class AppLovinRecyclerViewAdapter(appLovinUnitId: String, context: Context?, title: String)
class AppLovinRecyclerViewAdapter(appLovinUnitId: String, context: Context, title: String)
: GenericRecyclerViewAdapter(title) {

private val adView: MaxAdView = MaxAdView(appLovinUnitId, MaxAdFormat.MREC, context!!)
private val adView: MaxAdView = MaxAdView(appLovinUnitId, MaxAdFormat.MREC, context)
private val mListener: TeadsAdapterListener

init {
Expand Down Expand Up @@ -65,12 +66,7 @@ class AppLovinRecyclerViewAdapter(appLovinUnitId: String, context: Context?, tit
adView.viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
override fun onGlobalLayout() {
adView.viewTreeObserver.removeOnGlobalLayoutListener(this)
val params: ViewGroup.LayoutParams = adView.layoutParams

// Here the width is MATCH_PARENT
params.height = adRatio.calculateHeight(adView.measuredWidth)

adView.layoutParams = params
adView.resizeAdContainer(adRatio)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import tv.teads.teadssdkdemo.format.mediation.identifier.AdMobIdentifier
import tv.teads.teadssdkdemo.utils.BaseFragment

/**
* Display inRead as Banner within a RecyclerView using AdMob Mediation.
* inRead format within a RecyclerView
*
*/
class AdMobGridRecyclerViewFragment : BaseFragment() {
private lateinit var binding: FragmentRecyclerviewBinding
Expand All @@ -32,7 +33,7 @@ class AdMobGridRecyclerViewFragment : BaseFragment() {

val adUnit = AdMobIdentifier.getAdUnitFromPid(pid)

recyclerView.adapter = AdMobRecyclerViewAdapter(adUnit, context, getTitle())
recyclerView.adapter = AdMobRecyclerViewAdapter(adUnit, requireContext(), getTitle())
}

override fun getTitle(): String = "InRead AdMob RecyclerView Grid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import tv.teads.teadssdkdemo.format.mediation.identifier.AdMobIdentifier
import tv.teads.teadssdkdemo.utils.BaseFragment

/**
* Display inRead as Banner within a RecyclerView using AdMob Mediation.
* inRead format within a RecyclerView
*
*/
class AdMobRecyclerViewFragment : BaseFragment() {
private lateinit var binding: FragmentRecyclerviewBinding
Expand All @@ -32,7 +33,7 @@ class AdMobRecyclerViewFragment : BaseFragment() {

val adUnit = AdMobIdentifier.getAdUnitFromPid(pid)

recyclerView.adapter = AdMobRecyclerViewAdapter(adUnit, context, getTitle())
recyclerView.adapter = AdMobRecyclerViewAdapter(adUnit, requireContext(), getTitle())
}

override fun getTitle(): String = "InRead AdMob RecyclerView"
Expand Down
Loading

0 comments on commit f9922f4

Please sign in to comment.