When I click a resend button first time, the button will disable for 2 seconds.After 2 seconds the button will enable?
I am using this code
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setEnabled(false);
btn.postDelayed(new Runnable() {
public void run() {
btn.setEnabled(true);
Log.d(TAG,"resend1");
}
},1000);
}
});
But this code is not working properly.
try this for this purpose you can use Handler(import android.os.Handler;)
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
btn.setEnabled(true);
Log.d(TAG,"resend1");
}
},2000);// set time as per your requirement
}
});
You can use a timer for this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myButton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
myButton.setEnabled(true);
}
});
}
}, 5000);
}
});
Find the solution
In your button click
long mLastClickTime;
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
disableButtonTwoSecs();
// Here your implementation
}
});
public static boolean disableButtonTwoSecs() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 2000) {
return true;
}
mLastClickTime = SystemClock.elapsedRealtime();
return false;
}
Related
I am a beginner in Android. I have created a chronometer and it works. I added a new function (every 5 minutes a value 1000 shown in the textview, after 10 minutes it takes 2000). This function works but if I click a button exit and I re-open the chronometer, after 5 minutes the value continues to add to the previous value but I want to reset this value once I click exit. I have used
finish(); handler.removeCallbacks(null); handler.postDelayed(test, 100); but it doesn't seem to work. Thanks.
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (lastPause != 0) {
chronometer.setBase(chronometer.getBase() + SystemClock.elapsedRealtime() - lastPause);
} else {
chronometer.setBase(SystemClock.elapsedRealtime());
}
chronometer.start();
suppl();
btnStart.setEnabled(false);
btnStop.setEnabled(true);
btnExit.setEnabled(false);
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chronometer.stop();
onPause();
lastPause = SystemClock.elapsedRealtime();
btnStop.setEnabled(false);
btnStart.setEnabled(false);
btnExit.setEnabled(true);
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
handler.removeCallbacks(null);
handler.postDelayed(test, 100);
}
});
}
Handler handler = new Handler();
Runnable test;
public void suppl() {
test = new Runnable() {
#Override
public void run() {
count += 1000;
tprice.setText(String.valueOf(count));
handler.postDelayed(test, 5000);
}
};
handler.postDelayed(test, 5000);
}
public void onPause() {
super.onPause();
if (handler != null)
handler.removeCallbacks(test);
}
I would suggest using a TimerTask for this purpose.
While using time task you can cancel all the pending tasks by using cancel() on the timer.
if you have any doubts in this solution, let me know.
Instead use CountDownTimer
CountDownTimer CD = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
xo = (int) (millisUntilFinished / 1000);
secondsleftimg.setText("seconds remaining: " + millisUntilFinished / 1000);
Try this out :
Replace this line of code :
handler.removeCallbacks(null);
with this :
handler.removeCallbacksAndMessages(null); // it will remove all hadler
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
focus.setBase(SystemClock.elapsedRealtime());
handler.removeCallbacksAndMessages(null);
lastPause= 0;
}
});
Hi I have a button and a timer
I want to run a timer but when I click a button the timer should stop and the method test() should be called.
This is my code:
public class MainActivity extends AppCompatActivity {
Button button;
int a=1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
a=6;
}
});
new CountDownTimer(30000, 4000) {
#Override
public void onTick(long millisUntilFinished) {
if (a<5)
{
Toast.makeText(MainActivity.this, ""+a, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFinish() {
test();
}
}.start();
}
private void test ()
{
Toast.makeText(this, "test is run", Toast.LENGTH_SHORT).show();
}
}
You have to define it first:
CountDownTimer yourCountDownTimer = new CountDownTimer(30000, 4000) {
public void onTick(long millisUntilFinished) {
if (a<5)
{
Toast.makeText(MainActivity.this, ""+a, Toast.LENGTH_SHORT).show();
}
}
public void onFinish() {
test();
}
}.start();
yourCountDownTimer.cancel(); //To Stop Timer
using cancel key word
timer.cancel();
timer.purge();
private CountDownTimer timer;
timer=new CountDownTimer(30000, 4000) {
#Override
public void onTick(long millisUntilFinished) {
if (a<5)
{
Toast.makeText(MainActivity.this, ""+a, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFinish() {
test();
}
}.start();
call timer.cancel() on onClick() metod of your button Listener.
StartTimer() function Should call after button click event completed, but it is get executed before onclick event. ro how to stop timer before onclick event and start after onclick event.
How do I fix this?
btn1=(Button)findViewById(R.id.button);
btn2=(Button)findViewById(R.id.button2);
btn3=(Button)findViewById(R.id.button3);
timer = new Timer();
btn1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button1", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button2", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button3", Toast.LENGTH_SHORT).show();
}
});
}
public void StartTimer()
{
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
Log.v("timer", "Timer running");
}
}, 0, 5000);
Toast.makeText(getApplicationContext(), "Timer", Toast.LENGTH_LONG).show();
}
protected void onResume() {
super.onResume();
StartTimer();
}
}
There is already built in Timer class in android. So you can do like that:
import java.util.Timer;
Timer timer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timer.schedule(new TimerTask() {
#Override
public void run() {
Log.v("timer", "Timer running");
Toast.makeText(getApplicationContext(), "TIMER HAS FINISHED", Toast.LENGTH_SHORT).show();
}
}, 0, 5000);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button1", Toast.LENGTH_SHORT).show();
}
});
StartTimer();
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button2", Toast.LENGTH_SHORT).show();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
Toast.makeText(getApplicationContext(), "Button3", Toast.LENGTH_SHORT).show();
}
});
So just start timer in onCreate, and stop it when button clicked.
can any 1 give me example to perform onClick on button for multiple times when user clicked for 1 time. when i click on button 1 time it should automatically click after delay of 5 seconds for 100 times. how to perform that.
This is my sample code
mUnlock.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//It should be already ensured that this mSelectedLock is something user is authorized to access
if (mSelectedLock.unlock("RANDOM")) {
mUnlock.setVisibility(View.INVISIBLE);
mUnlock.postDelayed(new Runnable() {
public void run() {
mUnlock.setVisibility(View.VISIBLE);
}
}, 5000);
} else {
Toast.makeText(MainActivity.this, "Unable to unlock.", Toast.LENGTH_LONG).show();
}
}
});
#Override
public void onClick(View v) {actionToBeDone();startLoop(0);}
private void startLoop(final int i) {
if(i!=100) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Log.e("i",""+i);
actionToBeDone();
startLoop(i+1);
}
}, 2000);
}
}
private void actionToBeDone() {
//enter actions you want to be done
Log.e("actionToBeDone","Button Action");
}
int count = 0;
Timer timer = new Timer();
timer.schedule(new TimerTask(){
#Override
public void run(){
if(count < 100){
mUnlock.performClick();
}
}
}, 0, 5000);
5000 is the time in miliseconds you can +/- from here.
public class MainActivity extends Activity implements NetworkMonitorListener {
double _mylat = 0;
double _mylong = 0;
TextView textView1;
Button clcikbutton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
clcikbutton = (Button) findViewById(R.id.button1);
clcikbutton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
clcikbutton.setEnabled(true);
}
});
}
}, 5000));
clcikbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"MM/dd/yyyy hh:mm:ss aa");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
textView1.setText(DateFormat.getDateTimeInstance().format(
new java.util.Date("11/7/2014 5:19:11 AM UTC")));
}
});
}
}
this is my code for delay on button click.I am trying to implement that when i click on the button and after that it should disable for 5 seconds and then it should work.Please help me where i am doing wrong because there is Error coming .
Try to use Handler to disable button for given time :
clcikbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
textView1.setText(DateFormat.getDateTimeInstance().format(new java.util.Date("11/7/2014 5:19:11 AM UTC")));
clcikbutton.setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
clcikbutton.setEnabled(true);
}
},5000);
}
});
why not this:
clcikbutton = (Button) findViewById(R.id.button1);
clcikbutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
clcikbutton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
clcikbutton.setEnabled(true);
}
}, 5000));
}
});
There are two closing bracket after 5000 please check that.
Try like adding Some period 1000.
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
clcikbutton.setEnabled(true);
}
});
}
}, 5000, 1000);
You can use Handler to execute the line of code after some delay :
clickbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
clickbutton.setEnabled(true);
}
}, 5000);
}
});
Hope it will help you ツ
Timer is flaky, consider using Handler's postDelay() method. Try this:
clcikbutton = (Button) findViewById(R.id.button1);
clcikbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Do what you need to do..
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"MM/dd/yyyy hh:mm:ss aa");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
textView1.setText(DateFormat.getDateTimeInstance().format(
new java.util.Date("11/7/2014 5:19:11 AM UTC")));
// Set the button not-clickable..
clcikbutton.setEnabled(false);
// Then re-enable it after 5 seconds..
final Runnable enableButton = new Runnable() {
#Override
public void run() {
clcikbutton.setEnabled(true);
}
};
new Handler().postDelayed(enableButton, 5000);
}
});
Those code should get you what you want: It will make the button clickable at first, then disable it for 5 seconds the time you click it.
Hope this helped. :)
This is how you do it in Kotlin:
lifecycleScope.launch {
buttonTimer.isEnabled = false
delay(400) // debounce effect
buttonTimer.isEnabled = true
}