How To Count Number Of Button Clicks In Android
i'm building an android application and I need to count how many times a button is clicked within a specific time the display it in another page, any help?
Solution 1:
try this way, first declare global variable on your activity class file like below :
int clickcount=0;
after add click event to button and increment value clickcount variable like below code:
yourbutton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
clickcount=clickcount+1;
if(clickcount==1)
{
//first time clicked to do thisToast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();
}
else
{
//check how many times clicked and so on Toast.makeText(getApplicationContext(),"Button clicked count is"+clickcount, Toast.LENGTH_LONG).show();
}
}
});
Solution 2:
The answer given by @sandipon is fine.But global variables could be destroyed,you can use SharedPreferences to store the value in case you'd like to make it persistent.
Solution 3:
you can add clicks into List<Long/*timestamp*/>
and read values later, you can store your clicks into list and cleanup this list by timer. you can... you have many ways to do this. and save values into your database or preferences, if you need.
Post a Comment for "How To Count Number Of Button Clicks In Android"