'How do I show an admop interstitial ad after number of clicks on the item-views of recyclerview?
In some apps that are similar to my app, they have this thing when the user clicks on the item views of the recycler view randomly for 3 or 4 times and it displays an Admob interstitial ad for them before moving to the next activity or fragment. I would like to do the same, like showing the ad for the user after he clicks randomly for 3 or 4 times on the item views and when the ad is closed it should move to the next fragment. How do I do this kind of idea?
P.C. I am using navigation components to navigate between the fragments and you can use java or kotlin, I understand them both.
**Here is my recycler view code **
class NewsListAdapter(
val context: Context,
val news_list: ArrayList<NewsListViewModel>,
val view: View
) :
RecyclerView.Adapter<NewsListAdapter.vHolder>() {
var inum = 1
var countNum = 0
private lateinit var OverLoad: OverLoad
private lateinit var mRef: DatabaseReference
private var mInterstitialAd: InterstitialAd? = null
private final var TAG = "NewsListAdapter"
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): vHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.news_list_design, parent, false)
return vHolder(view)
}
override fun getItemCount(): Int {
return news_list.size
}
override fun onBindViewHolder(holder: vHolder, position: Int) {
val newsList = news_list[position]
val image = newsList!!.image
val imageView = holder.news_image
Glide.with(this.context)
.load(image)
.into(imageView)
holder.news_title.text = newsList.title
holder.news_desc.text = newsList.desc
holder.news_date.text = newsList.date
holder.news_type.text = newsList.type
val shared = context.getSharedPreferences("LatestNewsSaving", Context.MODE_PRIVATE)
val editor = shared.edit()
editor.putInt("fav_blue", R.drawable.ic_favorite_blue)
editor.putInt("fav_border", R.drawable.ic_favorite_border)
// editor.putInt("count_newsList_num", countNum)
editor.apply()
interstitialAd()
PushDownAnim.setPushDownAnimTo(holder.card_view)
.setOnClickListener {
countNum += 1
editor.putInt("count_newsList_num", countNum)
editor.apply()
if (shared.getInt("count_newsList_num", 0) == 3) {
countNum = 0
if (mInterstitialAd != null) {
mInterstitialAd?.show(context as Activity)
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.")
}
val passData = LatestNewsFragmentDirections
.actionLatestNewsFragmentToNewsDetailFragment(news_list[position])
Navigation.findNavController(view).navigate(passData)
} else {
val passData = LatestNewsFragmentDirections
.actionLatestNewsFragmentToNewsDetailFragment(news_list[position])
Navigation.findNavController(view).navigate(passData)
}
}
}
fun interstitialAd() {
var adRequest = AdRequest.Builder().build()
InterstitialAd.load(context, "ca-app-pub-xxxxxxxxxxxxxxxxx", adRequest, object :
InterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.message)
mInterstitialAd = null
}
override fun onAdLoaded(interstitialAd: InterstitialAd) {
Log.d(TAG, "Ad was loaded.")
mInterstitialAd = interstitialAd
}
})
}
class vHolder(view: View) : RecyclerView.ViewHolder(view) {
val news_image = view.findViewById<ImageView>(R.id.news_image)
val news_title = view.findViewById<TextView>(R.id.news_title)
val news_desc = view.findViewById<TextView>(R.id.news_desc)
val news_date = view.findViewById<TextView>(R.id.news_date)
val news_type = view.findViewById<TextView>(R.id.news_type)
val card_view = view.findViewById<CardView>(R.id.newsListCard)
val favourite_add = view.findViewById<ImageView>(R.id.favourites_add)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
