• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Button help

S12

Newbie
I want to know if their is way to check if a button is clicked X times do something. For example i have a button in my android app and if a user clicks it more than 3 times i want to show an ad.
 
You could use a class variable to record the number of clicks. This would be incremented every time the button is clicked. Like this

Code:
class myActivity extends Activity {

  private int numberOfClicks;
  private final int MAX_CLICKS = 3;
  ...

  button.setOnClickListener(new OnClickListener() { 
    @Override
    public void onClick(View view) {
      numberOfClicks++;
      if (numberOfClicks >= MAX_CLICKS) {
        // Show your ad
        numberOfClicks = 0;
      }  
    }   
  });
 
Back
Top Bottom