0

I wrote a CountDownTimer which Counts down a in the code fixed time when you click the start Button. The time is displayed in a TextView.

What I actually want is a field where the user can set the time manually. There should be a Field (or maybe 2 fields) to set minutes and seconds.

So, when a user sets "1" in field n.1 and "20" in field n.2, exactly 1 Minute and 20 seconds will be Count down.
I do not know how do that.

Code:

MainActivity.java:

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class MainActivity extends Activity {
    Button btnStart, btnStop;
    TextView textViewTime;
    @Override


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnStart = (Button)findViewById(R.id.btnStart);

        btnStop = (Button)findViewById(R.id.btnStop);
        textViewTime = (TextView)findViewById(R.id.textViewTime);
        textViewTime.setText("00:00:00");
        final CounterClass timer = new CounterClass(5000,1000);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
            }
        });
        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();

            }
        });
    }
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    public class CounterClass extends CountDownTimer {
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {

            textViewTime.setText("Time is over");





        }


        @SuppressLint("NewApi")
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override

        public void onTick(long millisUntilFinished) {
            long millis = millisUntilFinished;
            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            System.out.println(hms);
            textViewTime.setText(hms);
        }
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/nat">
    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_marginLeft="54dp"
        android:layout_alignTop="@+id/btnStop"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <Button
        android:id="@+id/btnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentRight="true"

        android:layout_centerVertical="true"
        android:layout_marginRight="67dp" />
    <TextView
        android:id="@+id/textViewTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF4444"
        android:layout_above="@+id/btnStop"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="17dp"
        android:textSize="25sp" />



</RelativeLayout>

Thank´s in advance.

  • 1
    Creating a time picker is [covered in the google android documentation](http://developer.android.com/guide/topics/ui/controls/pickers.html) – Oren Aug 11 '14 at 12:39
  • Thank you. I try to add this to my code. –  Aug 11 '14 at 12:42

1 Answers1

0

Use an EditText for each field and retrieve the values when start is clicked or use a TimePickerDialog.

0101100101
  • 5,786
  • 6
  • 31
  • 55
  • But when I use a timepicker I always have the AM/PM behind the time. ------ How can I retrieve the values when the btnStart Button is clicked ? –  Aug 11 '14 at 12:50
  • Use an OnTimeSetListener. onTimeSet() contains the parameter hourOfDay, which is independent of AM/PM (see http://stackoverflow.com/a/2660148/2964379). – 0101100101 Aug 11 '14 at 13:10
  • I could manage to find an even better post : http://stackoverflow.com/questions/21128310/user-input-and-timer-java-android-app , Thank´s for your help ! –  Aug 11 '14 at 13:25