Search code examples
androidonclicklistener

One button click, with a high degree of success


In the middle of implementing the app, I want to expose Admob ads on the 3rd click through the image save button. I'm writing because I don't know how. The function I want is to provide information about advertisements with a toast pop-up when the 1st and 2nd clicks are clicked. Admob ads are displayed on 3rd click.

And when I click again, I want to know how to return to the first one, can anyone help me! I'm a beginner, so I'm sorry for the lack of learning.

And thank you in advance.


Solution

  • Using a counter variable and modulo (%), I would do like this:

    private int clickCount;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        clickCount = 0;
    }
    
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.saveImage) {
            ++clickCount;
            switch(clickCount % 3) {
                case 0:
                    showAdMob();
                    break;
                default:
                    showToast();
            }
        }
    }