I have a problem with native ad: I want to use it in the fragment, that appears over my activity. Then user should swipe fragment from the screen with native ad on it. My fragment shows and hides with a swipe fine, my native ad block shows fine, but I cant make a swipe inside the native ad. I can swipe outside of ad only - just on fragment's space. So, native ad inercepts all touch events and nothing happens.
How can I make native ad to listen my swipes and leave clicks for its work as advertisement? I would like a user could swipe my fragment even by swiping on the ad block so far.
Native ad 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);
// listening for touch events
View ad_fragment = findViewById(R.id.ad_fragment);
ad_fragment.setOnTouchListener(new OnSwipeTouchListener(MainActivity.this) {
public void onSwipeTop() {
//
}
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;
}
}
public void onSwipeBottom() {
//
}
});
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>