Teh john
Teh john

Reputation: 41

CountDownTimer with notification

I have been searching for a while now but i haven't been able to find it done before. What i am aiming to do is to have the user able to set their own input time in a text field and then let them set a timer that will give them a notification once the time = 0

The closest thing that i could find is this which if i could get working i could simply change the time to a user input time, but with that i get an error on mTextField.

I assume what i am trying to do has been accomplished before but i just cant find any example of it.

Anyone got any useful links or some actual code of what it would look like?

Thanks in advance.

EDIT: At request here is what i tried, Simply made a new activity and imported the code:

public class TestTimer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timer_test);    

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

        }

    });     
}

timer_test:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

    <Button
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/timer" />
</LinearLayout>

Upvotes: 0

Views: 1228

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

That is because mTextField must exist in your XML but it is not there, just try to Log.d it instead, and see the result in your logcat

sample:

    public class TestTimer extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timer_test);

TextView timer=(TextView)findViewById(R.id.mTextField);                             
     CountDownTimer my_timer = new CountDownTimer(30000, 1000) {

         public void onTick(long millisUntilFinished) {
             timer.setText("Seconds remaining: "+  (millisUntilFinished / 1000));
         }

         public void onFinish() {
            timer.setText("Seconds remaining: 0 ");
         }

    };

        my_timer.start(); //call this to start the timer
}

Upvotes: 2

Related Questions