i am developing a game.i want to display score on button click.but it should be displayed only for few seconds.i want to implement timer in my app.but i dnt knw how to implement that.i searched in the google.but results were confusing me...given below is my code snippet.plz anybody help me...
OnClickListener clickball=new OnClickListener() {
#Override
public void onClick(View v) {
score=scorenumber.nextInt(9);
id=v.getId();
Log.v("", "u clicked me");
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
}
else if(id==R.id.ball3)
{
ball3.setText(Integer.toString(score));
}
else if(id==R.id.ball5)
{
ball5.setText(Integer.toString(score));
}
}
}
Something like
//Show score here
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//hide score here
}
}, 2000);
will hide your score after two seconds.
No need to go so deep in your case. Every view has postDelayed() method, that will run a custom code in UI thread after a set amount of time (in milliseconds). For example:
ball5.postDelayed(new Runnable(){
#Override
public void run() {
ball5.setText("");
}
}, 3000);
will clear "ball5" text after 3 seconds has passed
Another example
...
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
ball2.postDelayed(new Runnable(){
#Override
public void run() {
ball2.setText("");
}
}, 3000);
}
...
you can use CountDownTimer(); refer the documentation ,
Example :
new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
//hide your score here after 5 secondes (5000/1000)
}
}.start();
I think CountDownTimer is that what you need:http://developer.android.com/reference/android/os/CountDownTimer.html . It's quite simle for implementation
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
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;
}
});
I'm writing a android application. I have a CountDownTimer in between my application and i want until the timer to zero, all my app run(user must be select the true Button) and when become zero and the user did'nt anything, the other action performed (the user loses).
this is CountDownTimer :
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
a.setText("" + millisUntilFinished / 1000);
/*while (millisUntilFinished / 1000 != 1) {
}*/
}
public void onFinish() {
a.setText("done!");
}
}.start();
and this is part of code:
if ((randomAndroidColor1 == rainbow[0]) {
findViewById(ans).setBackgroundColor(rainbow2[0]);
findViewById(ans).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
c3.setBackgroundColor(rainbow1[2]);
}
});
}
How to access to millisUntilFinished variable in above if condition?
Timer and if condition worked correctly but I want to combine them.
If I understood correctly , you can use an instance boolean variable.
boolean isWon = false;
private void countDown() {
isWon=false; // Reset every method call.
// Set listener once in method.
findViewById(R.id.answer).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isWon=true;
}
});
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
// onTick works.
}
public void onFinish() {
if(isWon){
// Won
}else{
// Lose
}
}
}.start();
}
In my activity i want to check if countdowntimer is finished i'll stop the activity and pass to another activity?How can i do this?
I define countdowntimer like this
mcountdowntimer = new CountDownTimer(25000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i);
}
#Override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
}
};
mcountdowntimer.start();
mProgressBar.setProgressTintList(ColorStateList.valueOf(Color.rgb(64,91,164)));
}
Just simply add that logic in your onFinish() method:
#Override
public void onFinish() {
//here add the code for starting a new activity
}
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();
}
});
How can I add functionality to my activity that it automatically finishes after one minute?
I.e. I am creating a mathematical game where the user needs to get so many correct answers within a minute. I have created everything else but am unsure how to add the timer/Countdown?
I seen the following on the android website:
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
But I am unsure how to use this code, is it used within oncreate?
You can use Handler:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//finish is here
}
}, 60000);
}