I am new to android. I am learning to create a simple stop watch app. I got layout with three buttons and one textview. When I click start button, it will start the timer.
Single layout and single activity.
public void startTimer(View view){
running = true;
TextView textView = (TextView) findViewById(R.id.timer);
while(running) {
int hours = seconds / 3600;
int minutes = (seconds % 60) / 60;
int sec = seconds % 60;
String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
textView.setText(time);
seconds++;
if(seconds == 10){
running = false;
}
}
}
This is the method called when I click start button. I debugged the code. Values are generating properly. But not updating the layout.
Any suggestions?
I get the final result else one by one increase with the following addition
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stop_watch);
runTimer(); //I added this extra.
}
I will give you an alternate solution on your task,
public void startTimer(View view){
new CountDownTimer(10000, 1000) { //For 10 seconds
public void onTick(long seconds) {
String time = String.format("%02d : %02d ",
TimeUnit.MILLISECONDS.toMinutes(seconds),
TimeUnit.MILLISECONDS.toSeconds(seconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(seconds))
);
textView.setText(time);
}
}
public void onFinish() {
textView.setText("Finished");
}
}.start();
}
UPDATE :
public void startTimer(View view) {
running = true;
seconds = 0;
textView = (TextView) findViewById(R.id.txtView);
new Thread(new Runnable() {
#Override
public void run() {
while(running) {
runOnUiThread(new Runnable() {
#Override
public void run() {
int hours = seconds / 3600;
int minutes = (seconds % 60) / 60;
int sec = seconds % 60;
time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
textView.setText(time);
seconds++;
}
}) ;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(seconds==10){
running=false;
}
}
}).start();
}
Try using below code
public void startTimer(View view){
running = true;
TextView textView = (TextView) findViewById(R.id.timer);
while(running) {
int hours = seconds / 3600;
int minutes = (seconds % 60) / 60;
int sec = seconds % 60;
String time = String.format("%02d:%02d:%02d", hours, minutes, seconds);
seconds++;
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(time);
}
});
Thread.sleep(1000)
if(seconds == 10){
running = false;
}
}
}
Related
this is my first time here and i need ur help.
My activity starts calling this method, and theres a button that is supposed to restart the timer. The button contains the same method, but it doesn't restart. The method code has the timer.cancel in the end but it doesn't do the job, the timer keeps running and I cant get it. I really need a hand.
public final void starTimer() {
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String num = extras.getString("seg");
timerr = new CountDownTimer((Integer.parseInt(num)) * 1000 + 1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished / 1000;
if (millis<10){
txtTempo.setText("0"+ (millisUntilFinished / 1000)) ;
} else {
txtTempo.setText("" + (millisUntilFinished / 1000));
}
}
#Override
public void onFinish() {
txtTempo.setText("00");
}
});
}
};
timerr.cancel();
timerr.start();
}
Try this
public final void starTimer() {
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String num = extras.getString("seg");
if(timerr!=null){
timerr.cancel();
timerr=null;
}
timerr = new CountDownTimer((Integer.parseInt(num)) * 1000 + 1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished / 1000;
if (millis<10){
txtTempo.setText("0"+ (millisUntilFinished / 1000)) ;
} else {
txtTempo.setText("" + (millisUntilFinished / 1000));
}
}
#Override
public void onFinish() {
txtTempo.setText("00");
}
});
}
};
timerr.start();
}
Call timerr.cancel() onDestroy()/onDestroyView() in the Activity/Fragment.
or call separate method
timerr = new CountDownTimer((Integer.parseInt(num)) * 1000 + 1000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished / 1000;
if (millis<10){
txtTempo.setText("0"+ (millisUntilFinished / 1000)) ;
} else {
txtTempo.setText("" + (millisUntilFinished / 1000));
}
}
#Override
public void onFinish() {
txtTempo.setText("00");
}
});
}
};
timerr.start();
}
Use method whenever you want cancel the timer
cancelTimer();
void cancelTimer() {
if(timerr!=null)
timerr.cancel();
}
I am creating a countdown time in Android and I have the timer part working perfectly. However, I want to add a feature where the user can set the number of times they wish the timer to repeat. This is my current attempt at implementation
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
minutesText = (EditText) findViewById(R.id.minutesText);
secondsText = (EditText) findViewById(R.id.secondsText);
startButton = (Button) findViewById(R.id.startButton);
intervalCount = (EditText) findViewById(R.id.intervalCount);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (minutesText.getText().toString().equals("")) {
minutesInt = 0;
} else {
minutesString = minutesText.getText().toString();
minutesInt = Integer.parseInt(minutesString);
}
secondsString = secondsText.getText().toString();
if (secondsText.getText().toString().equals("")) {
secondsInt = 0;
} else {
secondsString = secondsText.getText().toString();
secondsInt = Integer.parseInt(secondsString);
}
intervalsString = intervalCount.getText().toString();
intervals = Integer.parseInt(intervalsString);
final int timerAmount = ((minutesInt * 60) + (secondsInt)) * 1000;
CountDownTimer timer = new CountDownTimer(timerAmount, 1000) {
#RequiresApi(api = Build.VERSION_CODES.N)
public void onTick(long millisUntilFinished) {
isRunning = true;
String timeLeft = String.format("%02d : %02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
);
textView.setText("Reamining: " + timeLeft);
}
public void onFinish() {
isRunning = false;
intervals--;
if (intervals > 0) {
timer.start();
}
try {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
};timer.start();
}
});
My error is in the onFinish()
it wont let me use timer.start()
CountDownTimer basically uses a Handler thread which is like other threads can not be restarted once its ended.
You need to use a new instance of the CountDownTimer in each interval.
So in your case, bring the
intervals--;
if (intervals > 0) {
// for each interval left create a new CountDownTimer here and start it.
}
outside of
new CountDownTimer(timerAmount, 1000 } {
// do your operations here.
}
once a timer start it couldn't be stop for 3 hours.if I click on backpress timer stoped.I am not sure how to pause and resume the timer as the textview.Please check my code.
TextView timer;
SharedPreferences mpref;
SharedPreferences.Editor ed;
String output;
MyCount counter;
long seconds;
long millisFinished;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start__test2);
mpref=getSharedPreferences("com.example.bright", Context.MODE_PRIVATE);
timer = (TextView) findViewById(R.id.timer);
//startService(new Intent(this, MyService.class));
counter = new MyCount(10800000, 1000);
counter.start();
}
countDownTimer method
public class MyCount extends CountDownTimer {
Context mContext;
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
Log.e("timeeeee", millisInFuture + "");
}
public void onTick(long millisUntilFinished) {
Log.e("time",millisUntilFinished+"");
millisFinished = millisUntilFinished;
timer.setText(formatTime(millisUntilFinished));
/* String timer_str = timer.getText().toString();
//SharedPreferences sp=
ed = mpref.edit();
ed.putString("time", timer_str);
ed.commit();*/
if (seconds == 0) {
}
}
public void onFinish() {
Toast.makeText(getApplicationContext(), "Time Up", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onResume() {
super.onResume();
/*// Log.e("valueeeee",millisFinished+"");
// new MyCount(millisFinished,1000);
// Log.e("value",millisFinished+"");*/
//counter
}
#Override
public void onDestroy() {
super.onDestroy();
// counter.cancel();
}
//================================================================================Time format
public String formatTime(long millis) {
output = "";
seconds = millis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 60;
String secondsD = String.valueOf(seconds);
String minutesD = String.valueOf(minutes);
String hoursD = String.valueOf(hours);
if (seconds < 10)
secondsD = "0" + seconds;
if (minutes < 10)
minutesD = "0" + minutes;
if (hours < 10)
hoursD = "0" + hours;
output = hoursD + " : " + minutesD + " : " + secondsD;
return output;
}
Please check my code
U need to use a service so that the timer runs even if the app is closed/destroyed. Try as below
public class TimerService extends Service {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private Context mContext;
public IBinder onBind(Intent intent)
{
return null;
}
public void onCreate()
{
super.onCreate();
mContext = this;
startService();
}
private void startService()
{
scheduler.scheduleAtFixedRate(runner, 0, 3, TimeUnit.HOURS);
}
final Runnable runner = new Runnable() {
public void run()
{
mHandler.sendEmptyMessage(0);
}
}
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}
private final Handler mHandler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
//do what ever you want as 3hrs is completed
}
};
}
If you create objects in your activities scope they will be disposed once the activity is gone because of the activity lifecycle.
The solution for running long background tasks is us inn services by IntentService.
You can read more about it here :
https://developer.android.com/training/run-background-service/create-service.html
Good luck!
I need count down timer that whenever I clicked or times up, send result and time score to another activity. So I used thread, but I don't know why in first time run activity it don't show time for some seconds in textview, one more strange thing is that when press back,then go to activity again it works fine.
count down code:
if (flagTime) {
flagTime = false;
new Thread(new Runnable() {
#Override
public void run() {
int counter = 60;
while (function.isActivityVisible() && counter > 0) {
counter--;
final int finalCounter = counter;
try {
Thread.sleep(1000);
G.HANDLER.post(new Runnable() {
#Override
public void run() {
if (function.isActivityVisible()) {
timeSc=finalCounter;
String preSec = "";
if (finalCounter < 10) {
preSec = "0";
}
if (function.isActivityVisible()
&& finalCounter <= 0) {
flagTime = false;
Intent intent = new Intent(
Memorize.this,
MemorizeResult.class);
intent.putExtra("TIME", 60);
intent.putExtra("ARRAY_RESULT",
array_choice);
startActivity(intent);
finish();
}
String score = "00:" + preSec
+ finalCounter;
txt[20].setText( " time ramaining " + score);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
if (!function.isActivityVisible()) {
flagTime = false;
finish();
}
}
}).start();
}
I use many flag to handle it, but it seems don't work properly.
Try using a CountDownTimer
Something like:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
I have a horizontal progress bar in android. I need to make it complete in 60 seconds.
Why doesn't the below code work?
int progress = 0;
progressBarHorizontal.setMax(60);
while(progress < 60) {
progressHandler.postDelayed((new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
}),progress * 1000);
progress++;
}
Please suggest some other methods. I tried this:
new Thread(new Runnable() {
int progress = 0;
public void run() {
long timerEnd = System.currentTimeMillis() + 60 * 1000;
while (timerEnd > System.currentTimeMillis() ) {
progress = (int) (timerEnd - System.currentTimeMillis()) / 1000;
// Update the progress bar
progressHandler.post(new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.w("tag","Progress thread cannot sleep");
}
}
}
}).start();
But it is not working either.
The second piece of code actually works, the problem was with my logic.
you can try the CountDownTimer :
pd = ProgressDialog.show(MovementEntry.this, "", "Please Wait...",
true, false);
pd.show();
new CountDownTimer(60000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
//this will be done every 1000 milliseconds ( 1 seconds )
int progress = (60000 - millisUntilFinished) / 1000;
pd.setProgress(progress);
}
#Override
public void onFinish() {
//the progressBar will be invisible after 60 000 miliseconds ( 1 minute)
pd.dismiss();
}
}.start();
This one will update the statusbar until it reaches its maximum value:
private int progress = 0;
private final int pBarMax = 60;
...
final ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1);
pBar.setMax(pBarMax);
final Thread pBarThread = new Thread() {
#Override
public void run() {
try {
while(progress<=pBarMax) {
pBar.setProgress(progress);
sleep(1000);
++progress;
}
}
catch(InterruptedException e) {
}
}
};
pBarThread.start();
You can try this code for that:
Thread timer = new Thread(){
public void run(){
try{
sleep(10000);
while(progressBarStatus < 10000){
StartPoint.this.runOnUIThread(new Runnable(){
public void run()
{
progressBar.setProgress(progressBarStatus);
progressBarStatus += 1000;
}
});
}
}catch(InterruptedException e){
e.printStackTrace();
}finally{
}
}
};
timer.start();
This code works for me
new Thread(new Runnable() {
public void run() {
long timerEnd = System.currentTimeMillis() + 60 * 1000;
while (timerEnd > System.currentTimeMillis()) {
progress = 60 - (int) (timerEnd - System.currentTimeMillis()) / 1000;
// Update the progress bar
progressHandler.post(new Runnable() {
public void run() {
progressBarHorizontal.setProgress(progress);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Log.w("App","Progress thread cannot sleep");
}
}
progressHandler.post(new Runnable() {
public void run() {
okButton.performClick();
}
});
}
}).start();