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.
Related
i have a while loop that i need to delay but i cant.
i have already tried two method but neither seem to work.
here's what i tried. I need when i click the button mNavigate to start the loop and run it with delay. I declare above that mNavMode is false
mNavigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked NAV icon");
mNavMode = !mNavMode;
while (mNavMode){
Log.d(TAG, "while: running");
new CountDownTimer(10000, 2500) {
public void onFinish() {
Log.d(TAG, "onFinish: finish");
}
public void onTick(long millisUntilFinished) {
if (mNavMode){
getDevLoc();
}
Log.d(TAG, "onTick: ticked "+millisUntilFinished);
}
}.start();
}
}
});
mNavigate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: clicked NAV icon");
mNavMode = !mNavMode;
Handler handler1 = new Handler();
while (mNavMode){
handler1.postDelayed(new Runnable() {
#Override
public void run() {
getDevLoc();
}
}, 1000 );
Log.d(TAG, "while: running");
}
}
});
I've used the second option using a Handler, it should work. But it appears that your while condition is false, just before starting a loop you set mNavMode = !mNavMode, and if your mNavMode is set to true beforehand than your loop body is never executed.
I'm a newer programmer and this is my first project but I'm having a bit of trouble in making a proper loop with three timers that are supposed to run one after the other. I managed to get the objects to hold the values they are supposed to within the loop but for some reason, the timer isn't displaying in the text field like it should.
startBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("mTimer:", String.valueOf(mTimer));
Log.i("mReps:", String.valueOf(mReps));
Log.i("Flexion:", String.valueOf(flexionTimer));
Log.i("Hold:", String.valueOf(holdTimer));
Log.i("Extension:", String.valueOf(extensionTimer));
for (int iter = 0; iter < mReps; iter++) {
Log.i("Loop:", String.valueOf(iter));
final Timer workingFlexionTimer = new Timer();
workingFlexionTimer.schedule(new TimerTask() {
int counter = ((int) flexionTimer / 1000);
#Override
public void run () {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPhase.setText("Flexion");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingFlexionTimer.cancel();
}
}
}, 0, 1000);
final Timer workingHoldTimer = new Timer();
workingHoldTimer.schedule(new TimerTask() {
int counter = ((int) holdTimer / 1000);
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPhase.setText("Hold!!!");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingHoldTimer.cancel();
}
}
}, flexionTimer, 1000);
final Timer workingExtensionTimer = new Timer();
workingExtensionTimer.schedule(new TimerTask() {
int counter = ((int) extensionTimer / 1000);
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
mPhase.setText("Extension");
mCountDownTimer.setText("" + String.format(String.valueOf(counter + 1)));
}
});
if (counter-- == 0) {
workingExtensionTimer.cancel();
}
}
}, (flexionTimer + holdTimer), 1000);
}
I'm kind of at a loss at this point and any suggestion would be appreciated.
Use handler
private void Timer() {
handler = new Handler();
Run =new Runnable() {
#Override
public void run() { //Do something after 10 sec
Toast.makeText(getActivity(), "Timer called!",Toast.LENGTH_LONG).show();
Timer(); // Do again
}};
handler.postDelayed(Run , 10000); // 10 sec
}
UPDATE
For timers I always do:
final TextView textView = (TextView)findViewById(R.id.textView);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int counter = 10;
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(""+counter);
}
});
if (counter-- == 0){
timer.cancel();
}
}
}, 0, 1000);
timer.schedule(new TimerTask() {
int counter = 10;
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(""+counter);
}
});
if (counter-- == 0){
timer.cancel();
}
}
}, 10000, 1000);
timer.schedule(new TimerTask() {
int counter = 10;
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(""+counter);
}
});
if (counter-- == 0){
timer.cancel();
}
}
}, 20000, 1000);
For more info check this
In the link...
public void schedule(TimerTask task, long delay, long period)
The above code. Start the run() without delay.
long delay = 0;// in ms
long period = 1000;// in ms
So every 1000ms call the run() and counter--. When counter = 0 cancel.
If you want to run, the one after the other, put delay.
UPDATE
Now the first will run immediately, the second will wait 10000ms (10s) and will run, finaly the third will wait 20000ms (20s) and then run.
The first timer flexionCountDownTimer was started in a loop, which means it will be started more than one time if mReps is greater than 1. This might leads to 2nd run of flexionCountDownTimer before the 1st run finished. Is this your expected behavior?
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;
}
In my app I want to make a button invisible for a few seconds after another button has been pressed and then it should become visible again.
How it is possible?
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn.setVisibility(View.VISIBLE);
}
}, 1000); // where 1000 is equal to 1 sec (1 * 1000)
}
});
You can do somthing like this:
firstBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
secondBtn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
secondBtn.setVisibility(View.VISIBLE);
}
}, 2000); //change it for the time you need in milliseconds
}
});
must make buttonView invisible then use btnView.postDelayed
Just inside onClick of second button just do
secondButtonView.setVisibility(View.INVISIBLE);
secondButtonView.postDelayed(new Runnable() {
#Override
public void run() {
secondButtonView.setVisibility(View.VISIBLE);
}
}, 2000);
View.postDelayed() simply calls Handler.postDelayed(). It's a
convenient method that helps avoid creating Handler instances.
This quote is from Romain Guy Android framework engineer https://groups.google.com/forum/#!topic/android-developers/IuG3HgKx89Q
//my button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
//my button visible
}
}, 5000);
U can use handler for it
also u can use Timer and TimerTask
//First button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Second visible
// after some MS
}
}, 2000);
Suppose you have two buttons Button button1,button2 properly inflated and displayed in view. You can change visibility of button2 on click of button1 by:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button2.setVisibility(View.VISIBLE);
}
}, 2 * 1000);//number of seconds *1000
}
});
try this,
write following code on another button's click event.
continuebutton.setVisibility(View.INVISIBLE);
continuebutton.postDelayed(new Runnable() {
public void run() {
continuebutton.setVisibility(View.VISIBLE);
}
}, 2000);
I want to print sequential numbers every 2 seconds when I press button. I used following code:
int j=0;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
c=Calendar.getInstance();
Delay(2 ,c.get(Calendar.SECOND));
if(j++<5)
t.setText("number "+j);
}
});
public void Delay(int p,int q){
int z=0;
while(z<p){
c=Calendar.getInstance();
i= c.get(Calendar.SECOND);
z=i-q;
}
return ;
}
but this code prints "Number 5" at the end of 10 seconds directly.
How can I print "Number 1", "Number 2", "Number 3"....sequentially every 2 sec.
Note that if you're doing this on UI thread, you'll be blocking the UI thread for 10 seconds. It's much better to have a separate thread to do this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
new Thread() {
public void run() {
for(int j=1; j<=5; i++) {
runOnUiThread(new Runnable() {
#Override
public void run() { t.setText("number " + j); }
});
SystemClock.sleep(2000);
}
}
}.start();
}
});
This code starts a new thread (thus not blocking the UI), which iterates from 1 to 5 outputting the number (on UI thread, as it's changing the UI) and then sleeps for 2 seconds.
Use a Runnable to post to a handler bound to the UI thread for your app rather than sleep or delay. If you sleep or delay in your onClick() method, you are blocking the UI thread and it will make your UI unresponsive.
public class MyActivity extends Activity implements Handler.Callback {
...
private Handler mHandler = new Handler(this);
private int mNumber = 0;
...
#Override
public void onClick(View v) {
mNumber++;
mHandler.postDelayed(new Runnable() {
public void run() {
t.setText("number: " + mNumber);
}, 2000);
}
}
You can use the CountDownTimer for this..you just have to define the amount of time and how often you want to update. You just have to adjust the logic a bit to do print the numbers upward.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
new CountDownTimer(60000, 2000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
});