1

I have made an app, where the user presses a button to lead onto another activity, but I only want the user to press this button 5 times in total. I tried the below code, however it doesn't stop the user pressing the button more than 5 times, so I'm guessing the app needs to somehow remember the clicks?

public void FoodClicks(View view){ if(this.counter == 5){ this.counter ++; } else { Button btn = (Button)findViewById(R.id.button); btn.setEnabled(false); } } 

ActivityMain.xml:

<Button android:id="@+id/button" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="38dp" android:text="@string/addmeal" /> 
6
  • How do you use FoodClicks(View) in your button? Could you post your layout xml? Commented Mar 2, 2015 at 15:16
  • Where do you use this method in fragment or in Activity? Commented Mar 2, 2015 at 15:17
  • Have a look at stackoverflow.com/questions/4022830/… Commented Mar 2, 2015 at 15:17
  • @KonradKrakowiak Please see XML of button above. I'm using this method within an Activity. Commented Mar 2, 2015 at 15:28
  • @NicolaBA Let's try my solution Commented Mar 2, 2015 at 15:34

4 Answers 4

1
button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int clicks = 0; clicks++; if (clicks >= 5){ button.setEnabled(false); } SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", this.MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.putInt("clicks", clicks); editor.apply(); } }); 

Use the sharedPreferences to store number of clicks, so if your button perform any action, clicks can be stored.

Sign up to request clarification or add additional context in comments.

7 Comments

If the button starts a new activity each time, I doubt this will work because each time the counter will reset
@Eenvincible oh yes, button counter needs to be stored somewhere.
@Apurva yes I'm guessing that is why it isn't working for me. Any idea how I go about saving the counter?
@NicolaBA wait I try something
@Eenvincible see I used sharedPreferences to store number of clicks
|
1

You have to assign the increased number to your member field. Also the evaluation was not correct.

public void FoodClicks(View view){ if(this.counter < 5){ this.counter += 1; } else { Button btn = (Button)findViewById(R.id.button); btn.setEnabled(false); } } 

More over, what is the view argument? The actual button? Then you can use that instead directly.

public void FoodClicks(View view){ if(this.counter < 5){ this.counter += 1; } else { ((Button) view).setEnabled(false); } } 

1 Comment

I changed the code to yours above, however when I test the user can still click on the button more than 5 times.
1

since your using this button to open another activity so that will cause your activity to reload when you get back to it ...so the counter will be reInitialized one way to solve the problem is to store that counter on a save area like your Application class

2 Comments

I thought that might be the case. How would I go about saving the counter?
first you need to make sure that you've extended the application class as described here after that you go and place this counter that class as public static
1

You newer call your method, add this to your xml:

<Button ... android:onClick = "FoodClicks" /> 

or implement in your Activity in onCreate method listener which will call you code:

 Button btn = (Button)findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FoodClicks(v); } }); 

And additionally you have to change your condition from

this.counter == 5 

to

this.counter < 4 

And remember to initialise this.counter = 0; on your Activity

7 Comments

Read the OP's question carefully.
@Apurva but when he disable button there is no many possibility to change counter any more
@KonradKrakowiak So I have set the counter to 0 = 'public int counter = 0;' and I have added the code above to my XML. There are no errors, but when I run and test on an emulator, it is still not stopping the user click on the button after 5 clicks.
@NicolaBA Did you change the condition?
@NicolaBA Change condition to counter < 4
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.