1. Download our Official Android App: Forums for Android!

Apps Android Button help

Discussion in 'Android Development' started by S12, May 12, 2016.

  1. S12

    S12 Member
    Thread Starter
    Rank:
    None
    Points:
    16
    Posts:
    39
    Joined:
    Dec 24, 2015

    Dec 24, 2015
    39
    1
    16
    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.
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,878
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,878
    11,515
    1,988
    Male
    Software developer
    South West of England
    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 (Text):
    1.  
    2. class myActivity extends Activity {
    3.  
    4.   private int numberOfClicks;
    5.   private final int MAX_CLICKS = 3;
    6.   ...
    7.  
    8.   button.setOnClickListener(new OnClickListener() {
    9.     @Override
    10.     public void onClick(View view) {
    11.       numberOfClicks++;
    12.       if (numberOfClicks >= MAX_CLICKS) {
    13.         // Show your ad
    14.         numberOfClicks = 0;
    15.       }  
    16.     }  
    17.   });
    18.  
     

Share This Page

Loading...