I've been working on getting a timepicker dialog to set my countdown timer. I have tried many ways with many different results... none of them worked right. I decided to experiment with some basic code which I will put below.
I cannot get the selected time (I called it selectedStartTime in the code below) to pass through into the actual countdown timer as startTime. I can only get the timer to work right when I use the preset startTime = 10000 (or any number). I don't want the startTime to be a set number. I need it to come from the OnTimeSetListener in the time picker box.
I will be forever grateful to anyone that can show me how to change the code so that the selected time in the dialog box is actually used in the timer.
Complete XML code for: activity_simple_timer_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dip">
<TableRow>
<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:text="Time: " />
<TextView
android:id="@+id/timeElapsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:text="Time elapsed: " />
</TableRow>
</TableLayout>
</LinearLayout>
Complete Java code for: SimpleTimerTest.java
package com.YOURPACKAGE INFO;
import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class SimpleTimerTest extends Activity implements OnClickListener {
private MalibuCountDownTimer countDownTimer;
private long timeElapsed;
private boolean timerHasStarted = false;
private Button startB;
private TextView text;
private TextView timeElapsedView;
int selectedStartTime;
// don't want to use a predefined startTime
private final long startTime = 10000;
private final long interval = 1000;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_timer_test);
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));
}
TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
selectedStartTime = ((hourOfDay*3600000)+(minute*60000));
// I can't get this variable to pass through into the countdown timer
}
};
@Override
public void onClick(View v) {
TimePickerDialog d = new TimePickerDialog(SimpleTimerTest.this, onTimeSetListener, 1, 0, true);
d.setTitle("Pick Sleep Duration hour:min");
d.setCancelable(true);
d.setButton(DialogInterface.BUTTON_POSITIVE, "Start", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("Stop");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("Timer was already running");
}
}
}
});
d.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_NEGATIVE) {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("Restart");
}
}
});
d.show();
}
public class MalibuCountDownTimer extends CountDownTimer {
public MalibuCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish() {
text.setText("Time's up!");
timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
Toast toast = Toast.makeText(getApplicationContext(), "finished", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
}
}
}
I'm not completely sure what you're trying to achieve, just create instance of your MalibuCountDownTimer
in method onTimeSet
, this method will be called only when user selected time.