Reputation: 77
I have an app that tells me how many hours to go at school ( :P ). I have a textview with the text Time: ( Hours to go ). This should be 6.2 but it is 6 at the moment, then when I click start it says School finished instantly and then crashes. Oh, I want to show the time in hours, like this ( 6.2 = 6 hours 20 minutes ). What could be the problem?
Code startActivity:
package com.nieeli.timer;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class startActivity extends Activity implements OnClickListener {
private static final String tag = "Main";
private MalibuCountDownTimer countDownTimer;
private long timeElapsed;
private boolean timerHasStarted = false;
private Button startB;
private TextView text;
private TextView timeElapsedView;
private final long startTime = 22500000/3600000;
private final long interval = 1/100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
timeElapsedView = (TextView) this.findViewById(R.id.timeElapsed);
countDownTimer = new MalibuCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime));
}
public void onClick(View v) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("Start");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("Stop and Reset");
}
}
// CountDownTimer class
public class MalibuCountDownTimer extends CountDownTimer {
public MalibuCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
text.setText("School Finished!");
timeElapsedView.setText("Time Passed: "
+ String.valueOf(startTime));
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
timeElapsedView.setText("Time Left: "
+ String.valueOf(timeElapsed));
}
}
}
Haven't made any changes in manifest or layout.
Thanks :D
Upvotes: 1
Views: 1082
Reputation: 7860
First, remember that long is an integer type. private final long interval = 1/100; Is not equivalent to 0.01; I believe it would work out to zero. Also, this is how often you want your onTick to be called; I believe you're trying to say you want to be called every 0.01 milliseconds, which seems excessive. Perhaps you meant every 100 milliseconds (ten times a second)?
You may also want to swap the startB.setText lines so that when it is stopped it shows "Start" and when it is started it shows "stopped"
You want to convert milliseconds to hours and minutes? You could try something like this: long totalMinutes = millis / 1000 / 60; long hours = millis / 60; long minutes = totalMinutes - (hours * 60);
EDIT: There are two main approaches if you want your countdown number (6.2 representing 6 hours and 20 minutes) to decrease by .1 every ten minutes.
The first option is to set "interval" to 10 * 60 * 1000 (the number of milliseconds in ten minutes). Then in onTick, take a float number (10.0 or whatever) and decrease by 0.1. That may be a quick and dirty method, if it even works, but it's not ideal.
The second is to do what I had described above. Then when you have the number of hours and number of minutes: float timeRemaining = (float) hours + (float) minutes / 100 use String.format to display it in the textview. Something like String.format("%.1f%n", timeRemaining) to show it with only one decimal point.
I didn't actually try the code--but off the top of my head, this should give you some tips to steer you in the right direction.
Upvotes: 3