Home > Mobile >  Issue :- Two click counters interacting with each other
Issue :- Two click counters interacting with each other

Time:06-30

I have 5 buttons and 2 menu button:

  1. First list
  • back,
  • random,
  • next
  1. Second list
  • copy,
  • share,
  • first quote,
  • latest quote

The menu buttons are the first quote and the latest quote

In the first list of buttons, I have added a counter of 10, which means after 10 clicks the interstitial ad should be shown.

In the second list of buttons, I have added a counter of 3, which means after 3 clicks the interstitial ad should be shown.

Now the problem comes when the first button counter reach 9 (means after one click it should shows the ad) and I press any button from 2) -> ad is showing

MainActivity.java

int triggerClicks = 3; // for copy,share,firstquote & latestquote
int countClicks = 0;
int triggerClicksNav = 10;// for back,next & random

@SuppressLint("SetTextI18n")
private void latestQuote() {
    countClicks  ;
    if (countClicks != 3) {
        position = quotes_list.size() - 1;
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position   "/"   quotes_list.size());
        Log.d(TAG, quotes_list.toString());
    }
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                position = quotes_list.size() - 1;
                quotesTxt.setText(quotes_list.get(position));
                countTxt.setText(position   "/"   quotes_list.size());
                Log.d(TAG, quotes_list.toString());
                countClicks = 0;
            }
        });
    }
}

@SuppressLint("SetTextI18n")
private void firstQuote() {
    countClicks  ;
    if (countClicks != 3) {
        position = 0;
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position   "/"   quotes_list.size());
        Log.d(TAG, quotes_list.toString());
    }
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                position = 0;
                quotesTxt.setText(quotes_list.get(position));
                countTxt.setText(position   "/"   quotes_list.size());
                countClicks = 0;
            }
        });
    }
}


@SuppressLint("SetTextI18n")
private void back() {
    countClicks  ;
    if (position > 0) {
        position = (position - 1) % quotes_list.size();
        quotesTxt.setText(quotes_list.get(position));
        countTxt.setText(position   "/"   quotes_list.size());
        Log.d(TAG, quotes_list.toString());
    }
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}

@SuppressLint("SetTextI18n")
private void next() {
    countClicks  ;
    position = (position   1) % quotes_list.size();
    quotesTxt.setText(quotes_list.get(position));
    countTxt.setText(position   "/"   quotes_list.size());
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}

@SuppressLint("SetTextI18n")
private void random() {
    countClicks  ;
    position = randomQ.nextInt(quotes_list.size());
    quotesTxt.setText(quotes_list.get(position));
    countTxt.setText(position   "/"   quotes_list.size());
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}


private void copy() {
    countClicks  ;
    ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = ClipData.newPlainText("text", quotesTxt.getText());
    if (clipboardManager != null) {
        clipboardManager.setPrimaryClip(clipData);
    }
    Toast.makeText(getApplicationContext(), "Copied", Toast.LENGTH_SHORT).show();
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
}

private void share() {
    countClicks  ;
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, quotesTxt.getText());
    startActivity(Intent.createChooser(intent, "Share to"));
    if (mInterstitialAd != null && countClicks >= triggerClicks) {
        mInterstitialAd.show(HomeActivity.this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                mInterstitialAd = null;
                intertitalAd();
                countClicks = 0;
            }
        });
    }
 }

The problem get solved if I assign a different counter for every buttons but I don't like this solution, please if u have another solution please add it

int countClicksBack = 0;
int countClicksRandom = 0;
int countClicksNext = 0;
int countClicksCopy = 0;
int countClicksShare = 0;
int countClicksFirst = 0;
int countClicksLatest = 0;

CodePudding user response:

The problem is that you're using the same counter to display your ad but, If I've understood, you want 2 different counter : one for buttons and one for menus. Right ? So, you have to declare 2 different counters.

By the way, your code is not "clean": the code used to display your ad can be exported in a own method, something like that:

private void showAd() {
   mInterstitialAd.show(HomeActivity.this);
   mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
      @Override
      public void onAdDismissedFullScreenContent() {
         super.onAdDismissedFullScreenContent();
         mInterstitialAd = null;
         intertitalAd();
         countClicks = 0;
      }
   });
}

private void back() {
    // your stuff
    if (mInterstitialAd != null && countClicks >= triggerClicksNav) {
        showAd();
    }
}
  • Related