I’m integrating AdMob Native ads into my Flutter app, and I’m facing an issue where tapping anywhere within the ad triggers the associated action. However, I only want the action to be invoked when the user presses the action button within the ad.
Here’s a snippet of my Android code related to displaying the native ad: I am android novice. I had compiled this code looking at the docs and using chatgpt.
feed_ad_1.xml
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#000000"
android:gravity="bottom"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginStart="34dip"
android:layout_marginEnd="34dip"
android:layout_weight="0.04"
android:gravity="end"
android:orientation="horizontal"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<TextView
android:id="@+id/ad_attribution_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:text="@string/ad_attribution_label2"
android:textAlignment="center"
android:textColor="@color/gnt_gray"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<com.google.android.gms.ads.nativead.MediaView
android:id="@+id/ad_media"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="center"
android:layout_weight=".3" />
<TextView
android:id="@+id/ad_headline"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.38"
android:fontFamily="@font/teko_regular"
android:gravity="start"
android:lineSpacingMultiplier="0.9"
android:paddingStart="34dip"
android:paddingTop="10dip"
android:paddingEnd="70dip"
android:paddingBottom="30dip"
android:textAlignment="textStart"
android:textColor="#FFFFFF"
android:textSize="40sp"
android:textStyle="normal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginStart="34dip"
android:layout_marginEnd="34dip"
android:layout_weight="0.04"
android:orientation="horizontal"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<TextView
android:id="@+id/ad_attribution_2"
android:layout_width="40dip"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/gnt_gray"
android:fontFamily="@font/montserrat_medium"
android:gravity="center"
android:text="@string/ad_attribution_label1"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/ad_advertiser"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginStart="14dip"
android:fontFamily="@font/montserrat_medium"
android:gravity="start|center"
android:textAlignment="textStart"
android:textColor="@color/gnt_gray"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<Button
android:id="@+id/ad_call_to_action"
style="@android:style/Widget.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="34dip"
android:layout_marginTop="27dip"
android:layout_marginEnd="28dip"
android:layout_marginBottom="37dip"
android:background="@drawable/button_rounded"
android:fontFamily="@font/montserrat_medium"
android:minHeight="52dip"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="#FFFFFF" />
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
FeedItemAdFactory.java
package com.myapp.app;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.ads.nativead.MediaView;
import com.google.android.gms.ads.nativead.NativeAd;
import com.google.android.gms.ads.nativead.NativeAdView;
import io.flutter.plugins.googlemobileads.GoogleMobileAdsPlugin.NativeAdFactory;
import java.util.Map;
class FeedItemAdFactory implements NativeAdFactory {
private final Context context;
FeedItemAdFactory(Context context) {
this.context = context;
}
@Override
public NativeAdView createNativeAd(
NativeAd nativeAd, Map<String, Object> customOptions) {
final NativeAdView adView =
(NativeAdView) LayoutInflater.from(context).inflate(R.layout.feed_ad_1, null);
adView.setMediaView((MediaView) adView.findViewById(R.id.ad_media));
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
String headlineText = nativeAd.getHeadline();
if (nativeAd.getBody() != null) {
headlineText += "n" + nativeAd.getBody();
}
((TextView) adView.getHeadlineView()).setText(headlineText);
adView.getMediaView().setMediaContent(nativeAd.getMediaContent());
if (nativeAd.getCallToAction() == null) {
adView.getCallToActionView().setVisibility(View.INVISIBLE);
} else {
adView.getCallToActionView().setVisibility(View.VISIBLE);
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
}
adView.getIconView()).setImageDrawable(nativeAd.getIcon().getDrawable());
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
adView.getAdvertiserView().setVisibility(View.VISIBLE);
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
}
adView.setNativeAd(nativeAd);
return adView;
}
}
Can someone please guide me on how to ensure that the action associated with the ad is only triggered when the user presses the action button within the ad, rather than anywhere within the ad?
Thank you in advance for any assistance!
I tried adding this piece of code that chatgpt recommended but it changes nothing.
// Wrap CTA button with GestureDetector for click handling
Button ctaButton = (Button) adView.getCallToActionView();
GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override // Remove this annotation
public boolean onTap(View v) {
// Replace with the correct method for getting the click URL from your AdMob library
launch(nativeAd.getClickToAction() /* or appropriate method name */);
return true;
}
});
ctaButton.setOnTouchListener(gestureDetector);
}
// Disable clickability for the NativeAdView
adView.setClickable(false);
adView.setEnabled(false);
// Disable clickability for other views
adView.getHeadlineView().setClickable(false);
adView.getMediaView().setClickable(false);
adView.getAdvertiserView().setClickable(false);
// Enable clickability for the call-to-action button
adView.getCallToActionView().setClickable(true);
adView.getCallToActionView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nativeAd.performClick(new Bundle());
}
});