Issue
I have a single activity app with a navigation component. On my app, I am trying to implement an AdMob native ad (with a test unitId from google developer docs), but it doesn't show up no matter how much I've tried and I read this document but I didn't understand how am I gonna display it with there code. I also tried looking for examples and tutorials but most of them are in languages I don't understand. Any help would be appreciated!.
My manifast
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="@string/appId"/> // it is a sample
my_nativead.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="3dp"
android:background="@drawable/linear_border_style">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ad"
android:textColor="@color/white"
android:background="@color/blue"
android:backgroundTint="@color/blue"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/nativeAdSmallImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_email"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/nativeAdSmallTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginStart="10dp"
android:textStyle="bold"
android:textColor="@color/black"
android:text="Ad Title"/>
<com.google.android.gms.ads.nativead.MediaView
android:id="@+id/nativeAdSmallMedia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/nativeAdSmallName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:text="Ad description"/>
<TextView
android:id="@+id/nativeAdSmallDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:layout_marginStart="10dp"
android:textColor="@color/black"
android:text="Ad description"/>
<Button
android:id="@+id/nativeAdSmallButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:text="visit site"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
My mainActivity
frame = findViewById(R.id.myAd)
MobileAds.initialize(this)
val adBuilder = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") // the unitId is a sample
.forNativeAd { nativeAd ->
desplayNativeAd(frame, nativeAd)
}
adBuilder.build()
}
fun desplayNativeAd(parent: ViewGroup, ad: NativeAd){
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
val adView = inflater.inflate(R.layout.native_small_adview, parent) as NativeAdView
val headlineView = adView.findViewById<TextView>(R.id.nativeAdSmallTitle)
headlineView.text = ad.headline
adView.headlineView = headlineView
val advertiser = adView.findViewById<TextView>(R.id.nativeAdSmallName)
advertiser.text = ad.advertiser
adView.advertiserView = advertiser
val imageAdView = adView.findViewById<ImageView>(R.id.nativeAdSmallImage)
Glide.with(this)
.load(ad.icon)
.into(imageAdView)
val mediaView = adView.findViewById<MediaView>(R.id.nativeAdSmallMedia)
adView.mediaView = mediaView
adView.setNativeAd(ad)
parent.removeAllViews()
parent.addView(adView)
}
My mainActivity.xml // i have a lot of xml code on this one but I only showed this, this is a framelyout that spposed to handle the the native ads
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@+id/myAd"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Solution
I just solved it on my on, I only called the frame layout from my my main fragment instead of my main activity and I wrote the adBuilder with it's function in the fragment's onViewCreated method.
Here is what I wrote..
// I wrote it in onViewCreated meathod
MobileAds.initialize(requireContext())
//native ad
val adBuilder = AdLoader.Builder(requireContext(), "your_ad_id")
.forNativeAd { nativeAd ->
val adView = layoutInflater.inflate(R.layout.native_big_adiew, null) as NativeAdView
displayBigNativeAd(adView, nativeAd)
frame.removeAllViews()
frame.addView(adView)
}
.build()
adBuilder.loadAd(AdRequest.Builder().build())
// outside onViewCreated method
private fun displayBigNativeAd(adView: NativeAdView, ad: NativeAd) {
val headline = adView.findViewById<TextView>(R.id.nativeAdBigTitle)
headline.text = ad.headline
adView.headlineView = headline
val advertiser = adView.findViewById<TextView>(R.id.nativeAdBigName)
advertiser.text = ad.advertiser
adView.advertiserView = adView
val icon = adView.findViewById<ImageView>(R.id.nativeAdBigImage)
icon.setImageDrawable(ad.icon.drawable)
adView.iconView = icon
val mediaView = adView.findViewById<MediaView>(R.id.nativeAdBigMediaView)
adView.mediaView = mediaView
val button = adView.findViewById<Button>(R.id.nativeAdBigButton)
adView.callToActionView = button
button.setText(ad.store)
val body = adView.findViewById<TextView>(R.id.nativeAdBigDesc)
body.text = ad.body
adView.bodyView = body
val price = adView.findViewById<TextView>(R.id.nativeAdBigPrice)
price.text = ad.price
adView.priceView = price
adView.setNativeAd(ad)
}
Answered By - Abood
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.