G
G
gr33tx2021-10-11 10:25:22
Android
gr33tx, 2021-10-11 10:25:22

How to make the native Admob block not intercept gestures?

Hello! I have a fragment in my application that will appear above the activity. I can hide it with a swipe, that is, by swiping to the side. Gestures set up on it, everything is OK. But the fragment should show an ad unit (native Admob). When you swipe on a block, gestures are ignored. Gestures only work outside the block, i.e. if you swipe across the space of the fragment. It turns out that the nav block intercepts all touch events.
Can you please tell me how to make it so that I can swipe on the ad block?

The ad unit uses a template

AdLoader adLoader = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
                .forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
                    @Override
                    public void onNativeAdLoaded(NativeAd nativeAd) {
                        // Show the ad.
                        Log.d("serg", "loaded");
                        NativeTemplateStyle styles = new
                                NativeTemplateStyle.Builder().withMainBackgroundColor(background).build();
                        TemplateView template = findViewById(R.id.my_template);
                        template.setStyles(styles);
                        template.setNativeAd(nativeAd);
                    }
                })
                .withAdListener(new AdListener() {
                    @Override
                    public void onAdFailedToLoad(LoadAdError adError) {
                        // Handle the failure by logging, altering the UI, and so on.
                        Log.d("serg", "fail"+adError.toString());
                    }
                })
                .withNativeAdOptions(new NativeAdOptions.Builder()
                        // Methods in the NativeAdOptions.Builder class can be
                        // used here to specify individual options settings.
                        .build())
                .build();

        // Creating  an Ad Request
        AdRequest adRequest = new AdRequest.Builder().build() ;

        // load Native Ad with the Request
        adLoader.loadAd(adRequest);


Fragment touch listener
// listening for touch events
View ad_fragment = findViewById(R.id.ad_fragment);
        ad_fragment.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
          
            public void onSwipeRight() {                
                fragmentManager.beginTransaction()
                        .setCustomAnimations(
                                R.anim.slide_in,  // enter
                                R.anim.slide_right_out  // exit
                        )
                        .hide(fragment1)
                        .disallowAddToBackStack()
                        .commit();
                toggleShowed = 0;

            }
            public void onSwipeLeft() {
                //Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
                if (toggleShowed == 0) {                   
                } else {                    
                    fragmentManager.beginTransaction()
                            .setCustomAnimations(
                                    R.anim.slide_in,  // enter
                                    R.anim.slide_left_out  // exit
                            )
                            .hide(fragment1)
                            .disallowAddToBackStack()
                            .commit();
                    toggleShowed = 0;
                }
            }         
        });

Fragment
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#000000"
    tools:context=".AdFragment">
    <LinearLayout
        android:id="@+id/adLayout"
        android:layout_gravity="center"
        android:text="gravity = center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:context=".MainActivity"
        tools:showIn="@layout/activity_main" >
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Ad"
            android:textColor="#FFDD00"
            android:textSize="19sp" />
        <!--  This is your template view -->
        <com.google.android.ads.nativetemplates.TemplateView
            android:id="@+id/my_template"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:gnt_template_type="@layout/gnt_medium_template_view" >
        </com.google.android.ads.nativetemplates.TemplateView>
    </LinearLayout>
</FrameLayout>

main activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activityMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".MainActivity">
    <androidx.fragment.app.FragmentContainerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/ad_fragment"
            android:name="ru.shutochki.AdFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="MissingConstraints" />
    </androidx.fragment.app.FragmentContainerView>
</androidx.constraintlayout.widget.ConstraintLayout>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question