gusain.jr
gusain.jr

Reputation: 76

Condition On Button click

When the button is clicked it should show me some text. This is working. Whenever the limit on the button click is exceeded, it must show some user defined text. After clicking the button 3 times, it is showing me some text not the user defined one. Here is my code for the OnClickListener:

final Button btnca =(Button) findViewById(R.id.btnca);
btnca.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int c=1;
        if(c <= 3) //if button click for first three times
        {
            new FancyShowCaseView.Builder(Playing.this)
                    .title(questionPlay.get(index).getCorrectAnswer())
                    .build()
                    .show();
            score -= 10;
            txtScore.setText(String.format("%d", score));
            c++;
        }
        if(c>3) //if button click for after three times
        {
            new FancyShowCaseView.Builder(Playing.this)
                    .title("Your Limit Exceed")
                    .build()
                    .show();
        }}
});

Upvotes: 2

Views: 1773

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21756

You should initialize counter variable c outside of onClick() method. Of-course you should initialize it as c = 0 instead of c = 1 to get the toast after 4th click.

Try this:

    final Button btnca =(Button) findViewById(R.id.btnca);
    btnca.setOnClickListener(new View.OnClickListener() {

        int c = 0; 

        @Override
        public void onClick(View v) {

            if(c <= 3) //if button click for first three times
            {
                new FancyShowCaseView.Builder(Playing.this)
                        .title(questionPlay.get(index).getCorrectAnswer())
                        .build()
                        .show();
                score -= 10;
                txtScore.setText(String.format("%d", score));
                c++;
            }
            if(c>3) //if button click for after three times
            {
                new FancyShowCaseView.Builder(Playing.this)
                        .title("Your Limit Exceed")
                        .build()
                        .show();

               // Reset if required
               //c = 0;

            }}
    });

FYI, if you want to reset variable c, then reset(c = 0) it inside your condition if(c>3).

Hope this will help~

Upvotes: 1

James McCracken
James McCracken

Reputation: 15776

The problem is that c is local to the onClick method so it is starting at 1 for every click. Try moving it out to the class level

final Button btnca =(Button) findViewById(R.id.btnca);
    btnca.setOnClickListener(new View.OnClickListener() {

        int c=1; //initialize here so it's re-used in each onClick

        @Override
        public void onClick(View v) {
            if(c <= 3) //if button click for first three times
            {
                new FancyShowCaseView.Builder(Playing.this)
                        .title(questionPlay.get(index).getCorrectAnswer())
                        .build()
                        .show();
                score -= 10;
                txtScore.setText(String.format("%d", score));
                c++;
            }
            if(c>3) //if button click for after three times
            {
                new FancyShowCaseView.Builder(Playing.this)
                        .title("Your Limit Exceed")
                        .build()
                        .show();
            }}
    });

Edit: I should mention this isn't a complete solution. I'll assume this code is in Activity(or Fragment).onCreate(). The counter will reset on configuration change when your lifecycle component is re-created, but I'll leave that solution as an exercise for the reader :)

Upvotes: 3

Related Questions