dimanche 8 octobre 2017

How to show CardView containing ads randomly but not before 3 cards and not after 7 cards?

I'm following this tutorial to show cards and integrate Tinder-like swipe feature.

In-between the cards I want to show ads and for that I'm using AdMob.

Here's the code:

@Layout(R.layout.ad_cards_view)
public class AdCards {

    @View(R.id.adView)
    NativeExpressAdView nativeExpressAdView;

    private Context mContext;
    private SwipePlaceHolderView mSwipeView;

    public AdCards (Context context, SwipePlaceHolderView swipePlaceHolderView) {
        mContext = context;
        mSwipeView = swipePlaceHolderView;
    }

    @Resolve
    private void onResolved() {
        AdRequest request = new AdRequest.Builder()
                .addTestDevice("***")
                .addTestDevice("***")
                .build();
        nativeExpressAdView.setVideoOptions(new VideoOptions.Builder()
                        .setStartMuted(true)
                .build());
        nativeExpressAdView.loadAd(request);
    }

}

Here's ad_cards_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://ift.tt/nIICcg"
    xmlns:ads="http://ift.tt/GEGVYd"
    xmlns:app="http://ift.tt/GEGVYd"
    android:orientation="vertical"
    android:layout_width="350dp"
    android:layout_height="395dp"
    android:layout_gravity="center"
    android:layout_marginTop="35dp">

    <android.support.v7.widget.CardView
        android:orientation="vertical"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        app:cardCornerRadius="7dp"
        app:cardElevation="4dp">

    <com.google.android.gms.ads.NativeExpressAdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        ads:adUnitId="ca-app-pub-xxx"
        ads:adSize="320x300">
    </com.google.android.gms.ads.NativeExpressAdView>

    </android.support.v7.widget.CardView>

</RelativeLayout>

Now I want to show these cards randomly in the stack but not before 3 cards and not after 7 cards.

Here's what I tried:

rand = new Random();
random = rand.nextInt(8-3) + 3;
count = rand.nextInt(8-3) + 3;

mSwipeView.addView(new Cards(mContext, profile, mSwipeView));
if (count >= random) {
    mSwipeView.addView(new AdCards(mContext, mSwipeView));
    random = rand.nextInt(8-3) + 3;
    count = rand.nextInt(8-3) + 3;
}

though this code is showing the cards containing ad randomly but it is not showing according to my needs and this card is appearing even after 1st card.

How can I make sure that this ad containing card appears randomly but not before 3 cards and not after 7 cards.




Aucun commentaire:

Enregistrer un commentaire