Flutter AdMob Native Ad: Action triggered on tap anywhere within ad, but want action only on button press

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());
            }
        });

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật