0

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://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • If I understood your question properly, you want a number between 3 and 7 (3 being the minimum and 7 being the maximum). According to my answer, [here](https://stackoverflow.com/a/21049922/2649012), the formula would be: `final int random = Random.nextInt((7 - 3) + 1) + 3;` or simplified for speed (one less addition to execute): `final int random = Random.nextInt((4) + 1) + 3;` – Phantômaxx Oct 09 '17 at 12:49
  • @ModularSynth I got how to get the random numbers in range of 3 to 7. What I want to know is the algorithm I should use to show `mSwipeView.addView(new AdCards(mContext, mSwipeView));` randomly not before 3 cards and not after 7 cards as stated in the question. I hope you got that. – Hammad Nasir Oct 09 '17 at 12:55
  • Then you aren't asking for the nextInt logic. You are asking how to manage the SwipeView custom control to follow the logic you're after. But I know nothing about this TinderSwipe. – Phantômaxx Oct 09 '17 at 12:59
  • @ModularSynth Nope. Please see the question. I'm trying to show that view using random numbers in which I tried to show it not before 3 cards and not after 7 cards. – Hammad Nasir Oct 09 '17 at 13:00
  • But you already have managed to het the random numbers. Now all you need to do is figuring out how to use them in the logical flow of your app, if that makes sense to you. – Phantômaxx Oct 09 '17 at 13:03
  • @ModularSynth Correct, but I'm unable to figure it out and thus this question. Please help me out. – Hammad Nasir Oct 09 '17 at 13:04
  • I actually gon't know how this custom control deals with cards. – Phantômaxx Oct 09 '17 at 13:07
  • Try to use for loop other wise there is should be listener with `mSwipeView` can solve this issue. –  Oct 15 '17 at 09:24
  • @Ibrahim I'm sorry but I didn't exactly get what you said. – Hammad Nasir Oct 15 '17 at 09:26
  • @HammadNasir i mean your library have this method to determine cards number `.setDisplayViewCount(3)` should be there some thing like `setStartCount(3)`, you may open an issue on the library site. –  Oct 15 '17 at 13:44

1 Answers1

0

If you know the cards to be put from before then what you can do is 1. create a card stack 2. put first two non-add-cards 3. Then make a random test if it has to put a add-card for third else put the non-add-card. 4. do this card put with test till 7 cards.

Example:

boolean atLeastOneAddPut = false;

int count = 1;
for(Data data: DataArray){
    if(count > 2 && count < 8 && shouldPutAdd()){
        mSwipeView.add(new AddView());
        count++;
    }
    mSwipeView.add(new NonAddView(data));
    count++;
}

private boolean shouldPutAdd(){
    //generate random 0 and 1
    int random;
    ...
    return random == 1;
}
Janishar Ali
  • 376
  • 3
  • 8
  • this would stop showing adView when the count increases 7. I want to show ads randomly just not before every 3 normal cards and not after every 7 normal cards. – Hammad Nasir Oct 16 '17 at 20:37
  • Refer this: https://github.com/janishar/Tutorials/tree/master/RandomPromotionalCards – Janishar Ali Oct 22 '17 at 20:48