Reputation: 1852
Currently I'm making a simple android app and I have one problem that I'm stuck for a while. I've read many articles over google on how to prevent multiple clicks with android , However, when I press the button there is no action taking place. All I want to do is to prevent users from clicking more than one time. I've posted the sample code. I wonder if there are some errors ...
private long mLastClickTime = 0;
public void sendData(View v) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
if ((number.getText().toString().equals("") || number.getText()
.toString() == null)
|| (num.getText().toString().equals("") || num.getText()
.toString() == null)) {
//alert the user
Toast.makeText(this, "Insertnumber",Toast.LENGTH_SHORT).show();
} else {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
//sending the data
trySendingData trying = new trySendingData();
trying.execute();
}
}
What I want to do is to execute the asyncTask trying.execute(); with one click.
Upvotes: 2
Views: 767
Reputation: 1249
As Piyush and SmulianJulian said Disable click and if you make it enable then enable it in end of async task. Like this:
public void sendData(View v) {
//First disable button so user cannot click it immediately
v.setEnable(false);
if ((number.getText().toString().equals("") || number.getText()
.toString() == null)
|| (num.getText().toString().equals("") || num.getText()
.toString() == null)) {
//alert the user
Toast.makeText(this, "Insertnumber",Toast.LENGTH_SHORT).show();
v.setEnable(true);//This makes view clickable
} else {
//sending the data
trySendingData trying = new trySendingData()
{
@Override
protected void onPostExecute(View result) {
//This makes view clickable after completing your asyncTask trySendingData
v.setEnable(true);
super.onPostExecute(result);
}
};
trying.execute();
}
}
Upvotes: 0
Reputation: 3103
you can call this method in onClickListener
public static void blockView(final View v) {
v.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
v.setEnabled(true);
}
}, 1000);
}
this will block the view for 1 second
Upvotes: 3
Reputation: 7131
There is multiple way to achieve this:
Method 1:
Set Tag.
Create the variable like
Boolean isClicked = false;
once your code reaches this
button.setonclick...{
if (!isClicked){
isClicked = true;
//Write your AsynkTask here
}
}
Method 2:
Disable button once clicked ex.:
button.setonclick...{
button.setEnabled(false);
//Write your AsynkTask here
}
}
Note : You are calling clcik action from xml so. You need to modify like this: Under this method:
public void sendData(View v) {
v.setEnabled(false);
}
I think the second is you need. I add method 1 for information
Upvotes: 0
Reputation: 609
You can disable button click once it is clicked by user btnSendData.setEnabled(false)
, start executing the asynctask with task to be performed. Once task is finished either you can enable button click on asynctask's onPostExecute() method or you can get result back where you want and enable the button click again by calling btnSendData.setEnabled(true)
Upvotes: 0
Reputation: 802
Just say
Whateverbuttoncallssenddata.setEnabled(false);
or
Whateverbuttoncallssenddata.buttsetOnClickListener(null);
as long as it's not a textview.
Upvotes: 0